← Back to MongoDB Course | Chapter 9: Schema Design | Lesson 5 of 7

Normalization vs denormalization

Normalizing avoids duplicate data while denormalizing duplicates some data to make reads faster.

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

bash
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
  1. Denormalizing data that changes constantly
  2. Never denormalizing anything
  3. 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

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.