When to Use Embedded vs Referenced Data Model in MongoDB
Embedded Data Model (Use When)
- Data is closely related
- Data is frequently read together
- One-to-few relationships
- Read performance is important
- Data size is small and manageable
{
name: "Manaswini",
orders: [
{ product: "Laptop", price: 50000 },
{ product: "Phone", price: 20000 }
]
}Referenced Data Model (Use When)
- Data is large or complex
- One-to-many or many-to-many relationships
- Data updates frequently
- Avoiding duplication is important
- Data is not always accessed together
// Users
{ _id: "u1", name: "Manaswini" }
// Orders
{ product: "Laptop", userId: "u1" }Key Differences
| Feature | Embedded | Referenced |
|---|---|---|
| Storage | Same document | Separate collections |
| Performance | Faster reads | Slower (multiple queries) |
| Data Duplication | Possible | Avoided |
| Best For | Simple data | Complex data |
Golden Rule
👉 If you read data together → Embed it
👉 If you update data frequently or it grows large → Reference it
Interview Points
- Embedded → fast reads, simple structure
- Referenced → scalable, avoids duplication
- Choose based on access pattern
- No one-size-fits-all approach