MongoDB relationships. What to use – embed or reference?

Technology CommunityCategory: MongoDBMongoDB relationships. What to use – embed or reference?
VietMX Staff asked 5 months ago
Problem

I want to design a question structure with some comments, but I don’t know which relationship to use for comments: embed or reference? Explain me pros and cons of both solutions?

In general,

  • embed is good if you have one-to-one or one-to-many relationships between entities, and
  • reference is good if you have many-to-many relationships.

Also consider as a general rule, if you have a lot of child documents or if they are large, a separate collection might be best. Smaller and/or fewer documents tend to be a natural fit for embedding.