开发者

Friendship relationship with MongoDB

开发者 https://www.devze.com 2023-03-05 07:57 出处:网络
I\'m new to MongoDB, and went with MongoMapper for some associations help. I\'m quite curious since, you see, I\'m trying to establish some User<->Friend relationships, and I\'m a little bit confu

I'm new to MongoDB, and went with MongoMapper for some associations help.

I'm quite curious since, you see, I'm trying to establish some User<->Friend relationships, and I'm a little bit confused about the difference between Document and EmbeddedDocument.

I suppose User would be a Document, but would Friend be an EmbeddedDocument for User or a Document on its own that simply gets called (many :friends) by User?

In my开发者_Go百科 preliminary design, a Friend's list would only be accessible through a User.

Thanks!


You're asking a basic "embed vs. reference" question that gets asked quite a bit when it comes to MongoDB. The answer is not always obvious.

Here's an extensive reply on a similar question. Here are the official MongoDB docs on this question.

One of the general rules:

"First class" objects, that are at top level, typically have their own collection.

In your case a Friend is probably a User object in itself. You probably don't want to Embed the entire Friend inside of the User. Instead, you probably want to keep a list of friends as an array inside of each User. (so probably the references)

0

精彩评论

暂无评论...
验证码 换一张
取 消