📖 Scenario: You are building a simple database for a library. Each book can have multiple reviews from readers. You want to store each book's information along with its reviews inside a single document to keep related data together.
🎯 Goal: Create a MongoDB collection where each book document contains an embedded array of review documents. This shows how to use the one-to-many embedding pattern in MongoDB.
📋 What You'll Learn
Create a collection named books with one book document.
The book document must have fields title and author with exact values.
Add an embedded array field reviews inside the book document.
Each review in reviews must have reviewer and comment fields.
Insert exactly two reviews with specified values.
💡 Why This Matters
🌍 Real World
Embedding related data like reviews inside a book document keeps all information together, making it faster to read and simpler to manage in many applications.
💼 Career
Understanding one-to-many embedding in MongoDB is essential for designing efficient NoSQL databases used in web development, content management, and many data-driven applications.
Progress0 / 4 steps
1
Create the books collection with one book document
Create a MongoDB document for the books collection with the fields title set to 'The Great Gatsby' and author set to 'F. Scott Fitzgerald'. Insert this document into the books collection.
MongoDB
Hint
Use db.books.insertOne() with an object containing title and author.
2
Add an empty reviews array field to the book document
Update the existing book document in the books collection by adding an empty array field called reviews. Use updateOne with a filter on title and the $set operator.
MongoDB
Hint
Use db.books.updateOne() with a filter on title and $set to add reviews: [].
3
Add two review documents inside the reviews array
Use updateOne with the $push operator and $each to add two review documents to the reviews array of the book with title 'The Great Gatsby'. The first review has reviewer 'Alice' and comment 'Loved the symbolism'. The second review has reviewer 'Bob' and comment 'A timeless classic'.
MongoDB
Hint
Use $push with $each to add multiple review objects inside the reviews array.
4
Verify the final embedded document structure
Write a query using findOne to retrieve the book document with title 'The Great Gatsby' from the books collection. This will show the embedded reviews array inside the document.
MongoDB
Hint
Use db.books.findOne() with a filter on title to see the full document including embedded reviews.
Practice
(1/5)
1. What is the main advantage of using the one-to-many embedding pattern in MongoDB?
easy
A. It separates related data into different collections for better normalization.
B. It duplicates data across multiple documents to improve write speed.
C. It requires complex joins to retrieve related data.
D. It stores related data inside the main document for faster reads.
Solution
Step 1: Understand embedding in MongoDB
Embedding means storing related data inside the main document instead of separate collections.
Step 2: Identify the advantage of embedding
This approach makes reading related data faster and simpler because all data is in one place.
Final Answer:
It stores related data inside the main document for faster reads. -> Option D
Quick Check:
Embedding = faster reads [OK]
Hint: Embedding means related data inside main document for quick access [OK]
Common Mistakes:
Confusing embedding with referencing (separate collections)
Thinking embedding requires joins
Assuming embedding duplicates data
2. Which of the following is the correct way to embed multiple comments inside a blog post document in MongoDB?
Hint: Use $push with full object to add to embedded array [OK]
Common Mistakes:
Using $set replaces whole array instead of adding
Using $addToSet with a string instead of object
Pushing array instead of object inside array
5. You want to store a user's orders in a single document using one-to-many embedding. Each order has an orderId, date, and a list of items. Which schema design is best to handle up to 100 orders per user efficiently?
hard
A. Separate orders collection with userId reference for each order
Step 1: Understand embedding for one-to-many with moderate size
Embedding up to 100 orders inside a user document is reasonable for fast reads.
Step 2: Check schema design for correct embedding
{ _id: userId, name: 'User', orders: [{ orderId, date, items: [{ productId, qty }] }] } embeds an array of order objects, each with its own items array, which fits the one-to-many embedding pattern well.