Embedding vs Referencing
In MongoDB, you can either embed related data within a document or reference it with an ID. The choice depends on your access patterns.
Example
// EMBEDDING — data inside the document
// Best for: data accessed together, 1:few relationships
{
"_id": ObjectId("..."),
"title": "My Blog Post",
"author": "Alice",
"comments": [
{ "user": "Bob", "text": "Great post!", "date": ISODate() },
{ "user": "Carol", "text": "Thanks!", "date": ISODate() }
]
}
// REFERENCING — store ID, look up separately
// Best for: large/growing data, many:many relationships
{
"_id": ObjectId("..."),
"title": "My Blog Post",
"author": ObjectId("user123"), // reference to users collection
"commentIds": [ObjectId("c1"), ObjectId("c2")]
} When to Embed vs Reference
Choose your modeling approach based on how you'll access the data.
- Embed when: data is always accessed together
- Embed when: child data belongs to exactly one parent
- Embed when: data doesn't grow unboundedly
- Reference when: data is accessed independently
- Reference when: many-to-many relationships exist
- Reference when: documents would exceed 16MB limit
