Embed or Reference
Embed or Reference: Embedding: Keeping Related Data Together, Referencing: Linking Documents by _id, Choosing by Relationship: One-to-Few, One-to-Many,…
Updated
- Embedding: Keeping Related Data Together (Apprentice): Embedding puts related data inside the document that uses it, like an order's line items and shipping address, so one read gets everything and one write saves it all at once, within a 16 MB limit per document.
- Referencing: Linking Documents by _id (Apprentice): Referencing links two documents by storing one's _id in the other, like a customerId in each order, so shared or changing data lives in one place, at the cost of a second query to follow the link.
- Choosing by Relationship: One-to-Few, One-to-Many, Many-to-Many (Journeyman): How many documents sit on each side of a relationship points to its model; embed a few, reference from the many side, never keep an array that grows without end, and link many-to-many with an array of ids.