Embedding & references
Understand the concept
Embedding stores related data inside one document, while referencing stores a link to data in another document. Embedding makes related reads and single-document atomic updates convenient, but can duplicate data or create oversized documents. Referencing supports independently managed data at the cost of additional reads or joins.
Important ideas
-
Embed small related data that is normally read together, such as an order address.
-
Reference large, shared or independently changing data, such as products.
-
Avoid arrays that grow forever; one document has a 16 MiB size limit.
-
Ask whether related records grow without a bound, change independently or must be updated together.
Work through the example
const order = {
customerId: 'customer-42', // Reference to an independent record.
shippingAddress: { city: 'Surat', postalCode: '395001' } // Snapshot.
};- The order keeps its delivery address even if the customer later edits their profile.
Can you explain it without the notes?
When should I embed data or reference it?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Embedding stores related data inside one document, while referencing stores a link to data in another document. Embedding makes related reads and single-document atomic updates convenient, but can duplicate data or create oversized documents. Referencing supports independently managed data at the cost of additional reads or joins.
Turn this concept into a clear answer.
- Base the choice on read/write patterns and lifecycle, not a rule that MongoDB always prefers embedding.
Keep connecting the ideas.
Next lesson: How do I model relationships and schema changes?Adapted from 04_MONGODB_MONGOOSE.md · “When should I embed data or reference it?”. Examples are study fragments; framework behavior can depend on your version.