2

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 preliminary design, a Friend's list would only be accessible through a User.

Thanks!

Andrés Botero
  • 1,042
  • 2
  • 10
  • 26

1 Answers1

1

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)

Community
  • 1
  • 1
Gates VP
  • 44,957
  • 11
  • 105
  • 108
  • 1
    Wow, that first link was really, really helpful! What a detailed answer! Thank you very much! Oh, and yes, I ended up doing precisely the same thing. I declared like this = many :friends, :in => :friends_ids, :class_name => 'User' – Andrés Botero May 15 '11 at 04:04