0
0
MongoDBquery~30 mins

Normalization vs denormalization default in MongoDB - Hands-On Comparison

Choose your learning style9 modes available
Understanding Normalization vs Denormalization in MongoDB
📖 Scenario: You are working on a simple online bookstore database using MongoDB. You want to understand how to organize your data using normalization and denormalization techniques.
🎯 Goal: Build two collections: one using normalization (separate collections with references) and one using denormalization (embedding related data). Learn how to create these structures step-by-step.
📋 What You'll Learn
Create a books collection with book details
Create an authors collection with author details
Create a normalized structure by referencing authors in books
Create a denormalized structure by embedding author details inside books
💡 Why This Matters
🌍 Real World
Many real-world applications use MongoDB to store data. Understanding when to normalize or denormalize data helps design efficient and easy-to-use databases.
💼 Career
Database developers and backend engineers often decide how to structure data for performance and maintainability. Knowing normalization and denormalization is essential for these roles.
Progress0 / 4 steps
1
Create the authors collection
Create a collection called authors with these exact documents: { _id: 1, name: "Jane Austen" } and { _id: 2, name: "Mark Twain" }.
MongoDB
Need a hint?

Use insertMany to add multiple documents to the authors collection.

2
Create the books collection with author references (Normalization)
Create a collection called books with these exact documents: { title: "Pride and Prejudice", author_id: 1 } and { title: "Adventures of Huckleberry Finn", author_id: 2 }. Use author_id to reference authors.
MongoDB
Need a hint?

Use author_id to link books to authors by their _id.

3
Create the books_denormalized collection with embedded author details (Denormalization)
Create a collection called books_denormalized with these exact documents: { title: "Pride and Prejudice", author: { name: "Jane Austen" } } and { title: "Adventures of Huckleberry Finn", author: { name: "Mark Twain" } }. Embed author details inside each book document.
MongoDB
Need a hint?

Embed the author object inside each book document instead of using a reference.

4
Add an index on author_id in books collection
Create an index on the author_id field in the books collection to speed up queries that find books by author.
MongoDB
Need a hint?

Use createIndex on the author_id field in the books collection.