Two Ways to Relate Data
Document databases offer two modeling choices for related data. You can embed a child inside its parent, or you can reference it by storing an id that points to another document.
When to Embed
- The child is owned by and read with the parent, such as line items inside an order.
- The data is read together far more often than written separately.
- The combined document stays well under the document size limit.
Embedding gives single read access to a whole object and keeps related data atomically updatable.
When to Reference
- The related entity is large, shared, or grows without bound.
- You need to query the child on its own or update it independently.
- Many parents point to the same child, so duplication would be costly.
References mimic the normalized relational style but require an extra lookup, often done with the aggregation join stage.
Key idea
Embed data that is owned and read with its parent for one read access, and reference data that is large, shared, or queried independently to avoid duplication.