1.Embedded documents vs. references
Embedding puts related data directly inside a document; referencing stores just an _id and looks the related document up separately. Embed what is read together and bounded; reference what grows independently or is shared broadly.
Key features
- Embedding nests related data inside the parent document, no join needed
- Referencing stores an _id and requires a second query (or $lookup) to resolve
- Embed when data is read together, bounded in size, and owned by one parent
- Reference when data grows independently, is shared, or has its own lifecycle
- The choice can be mixed per relationship — not all-embed or all-reference
Embed or reference — the deciding questions
- Embed
- Nested inline, no join
- Read together, bounded
- Owned by one parent
- Reference
- Stores an _id, needs a lookup
- Grows independently or shared
- Has its own lifecycle
Command
// Embed: { "parent": { "child": { ... } } }
// Reference: { "parent": { "childId": ObjectId("...") } }Embed data that is read together, bounded and owned by one parent. Reference data that grows independently or is shared across many parents.