Normalization vs denormalization
Normalizing avoids duplicate data while denormalizing duplicates some data to make reads faster.
In this page:
Normalization vs denormalization
Normalized models keep each fact in one place, which keeps updates simple but needs joins. Denormalized models copy data so a single read returns everything, which speeds reads but requires updating every copy.
MongoDB models usually denormalize the read-heavy parts and normalize data that changes often.
Note:
Denormalize fields that rarely change, like a product name on an order line.
Example: Normalization vs denormalization
test> db.products.insertOne({ _id: "P1", name: "Lamp", price: 45 })
{ acknowledged: true, insertedId: 'P1' }
// Denormalised order line: copies the name and price at the time of purchase
test> db.orders.insertOne({ _id: 1, items: [{ productId: "P1", name: "Lamp", price: 45, qty: 2 }] })
{ acknowledged: true, insertedId: 1 }
test> db.products.updateOne({ _id: "P1" }, { $set: { price: 50 } })
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }
test> db.orders.findOne({}, { items: 1, _id: 0 })
{ items: [ { productId: 'P1', name: 'Lamp', price: 45, qty: 2 } ] }
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Denormalizing data that changes constantly
- Never denormalizing anything
- Forgetting to update all copies
Chapter Summary
- Normalize for single-place updates
- Denormalize for fast reads
- Balance by change frequency
- Update every copy when needed
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: