0
I am designing a data model for an e-commerce platform in MongoDB. When structuring one-to-many relationships (like user addresses or order items), how do I decide whether to embed subdocuments or reference separate collections?
Decision Matrix
Choosing between embedding and referencing depends on query patterns and document size limitations:
- Embed when: You have "contains" relationships, bounded one-to-few subdocuments, and the data is frequently retrieved together.
- Reference when: You have one-to-many or many-to-many relationships, unbounded growth, or when subdocuments are updated independently and frequently.
Example: Embedded Schema Representation
{
"_id": ObjectId("60d5ec49f1a2c3456789abcd"),
"name": "John Doe",
"addresses": [
{
"street": "123 Main St",
"city": "New York",
"zip": "10001"
}
]
}