📖 Scenario: You are building a simple online bookstore database using MongoDB. You want to understand how denormalization works by embedding author details directly inside book documents instead of referencing them separately.
🎯 Goal: Create a MongoDB collection with books that embed author information. Then add a configuration variable to control whether to embed or reference authors. Finally, write a query to retrieve books with embedded author details.
📋 What You'll Learn
Create a books collection with embedded author details
Add a variable embedAuthors to control embedding
Write a query to find all books with embedded author names
Add a final step to show how to switch between embedding and referencing
💡 Why This Matters
🌍 Real World
Denormalization by embedding related data is common in MongoDB to improve read performance by reducing the need for joins.
💼 Career
Understanding when to embed or reference data is key for designing efficient NoSQL databases in real-world applications.
Progress0 / 4 steps
1
Create the books collection with embedded authors
Create a MongoDB collection called books with two documents. Each document must have a title field and an embedded author field. The author field should be an object with name and birthYear. Use these exact values: First book: title "The Great Gatsby", author name "F. Scott Fitzgerald", birthYear 1896. Second book: title "1984", author name "George Orwell", birthYear 1903.
MongoDB
Hint
Use db.books.insertMany() with an array of objects. Each object should have title and author fields. The author is an embedded object.
2
Add a variable embedAuthors to control embedding
Create a JavaScript variable called embedAuthors and set it to true. This variable will control if authors are embedded or referenced.
MongoDB
Hint
Use const embedAuthors = true to create the variable.
3
Write a query to find all books with embedded author names
Write a MongoDB query to find all documents in the books collection where the author.name field exists. Use db.books.find() with a filter that checks for author.name.
MongoDB
Hint
Use db.books.find({ "author.name": { $exists: true } }) to find books with embedded author names.
4
Add a final step to show switching between embedding and referencing
Add an if statement that checks if embedAuthors is true. If true, assign the embedded author object to a variable called bookAuthor. Otherwise, assign a reference ID string "authorId123" to bookAuthor.
MongoDB
Hint
Use if (embedAuthors) { bookAuthor = {...} } else { bookAuthor = "authorId123" } to show the trade-off.
Practice
(1/5)
1. What is a main advantage of denormalization in MongoDB?
easy
A. Faster read operations by storing related data together
B. Simpler updates with no data duplication
C. Reduced storage space by avoiding repeated data
D. Automatic data consistency without extra effort
Solution
Step 1: Understand denormalization purpose
Denormalization stores related data together to speed up reading.
Step 2: Compare options with this purpose
Only Faster read operations by storing related data together mentions faster reads by storing data together, which matches denormalization benefits.
Final Answer:
Faster read operations by storing related data together -> Option A
Quick Check:
Denormalization = Faster reads [OK]
Hint: Denormalization speeds up reads by grouping data [OK]
Common Mistakes:
Confusing denormalization with normalization benefits
Thinking denormalization reduces storage space
Assuming denormalization simplifies updates
2. Which MongoDB document structure shows denormalization?
What happens if Bob moves to 'LA' and you update only the city in this document?
medium
A. Update fails due to embedded document structure
B. All references to Bob's city update automatically
C. Only this document's city changes; duplicates elsewhere stay old
D. MongoDB merges old and new city values
Solution
Step 1: Understand update impact in denormalization
Denormalization duplicates data, so updating one copy doesn't affect others.
Step 2: Analyze update effect on embedded address
Updating city in one document changes only that document; duplicates elsewhere remain unchanged.
Final Answer:
Only this document's city changes; duplicates elsewhere stay old -> Option C
Quick Check:
Update one copy, duplicates unchanged [OK]
Hint: Updates affect only one copy in denormalized data [OK]
Common Mistakes:
Assuming all duplicates update automatically
Thinking embedded documents prevent updates
Believing MongoDB merges conflicting values
4. You have duplicated user phone numbers in multiple documents due to denormalization. Which is a common problem you might face?
medium
A. Updating phone number requires changing all documents manually
B. MongoDB automatically syncs all duplicates on update
C. Phone numbers cannot be stored in embedded documents
D. Denormalization prevents querying phone numbers
Solution
Step 1: Identify update challenges with duplicated data
Duplicated data means updates must be applied to every copy to keep consistency.
Step 2: Evaluate options about update behavior
Only Updating phone number requires changing all documents manually correctly states that manual updates to all duplicates are needed.
Final Answer:
Updating phone number requires changing all documents manually -> Option A
Quick Check:
Manual update needed for duplicates [OK]
Hint: Duplicates need manual updates to stay consistent [OK]
Common Mistakes:
Believing MongoDB auto-syncs duplicates
Thinking denormalization forbids embedded fields
Assuming denormalization blocks queries
5. You want to design a MongoDB schema for a blog where posts have many comments. You expect many reads but few comment edits. Which denormalization approach is best?
hard
A. Duplicate comments in both posts and a separate collection
B. Store comments in a separate collection and reference post IDs
C. Store only comment IDs inside posts without comment data
D. Embed all comments inside each post document
Solution
Step 1: Analyze read vs update frequency
Many reads and few updates favor denormalization to speed up reads.