Friendship relationship with MongoDB
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)
精彩评论