📖 Scenario: You are building a simple database for a small library. Each book has exactly one author, and you want to store the author details inside the book document itself to keep related data together.
🎯 Goal: Create a MongoDB collection called books where each book document embeds exactly one author document inside it. You will first create the book documents, then add author details as embedded documents, and finally query the collection to retrieve books with their authors.
📋 What You'll Learn
Create a books collection with at least two book documents containing only the book title and ISBN.
Add an embedded author document inside each book document with fields name and birthYear.
Write a query to find all books and show their embedded author details.
Use the one-to-one embedding pattern to keep author data inside the book document.
💡 Why This Matters
🌍 Real World
Many applications store related data together in one document to simplify data access and improve performance, especially when the relationship is one-to-one.
💼 Career
Understanding embedding patterns in MongoDB is essential for designing efficient NoSQL databases used in web development, data engineering, and backend services.
Progress0 / 4 steps
1
Create the initial books collection with book documents
Insert two documents into the books collection with these exact fields and values: { title: "The Great Gatsby", isbn: "9780743273565" } and { title: "1984", isbn: "9780451524935" }.
MongoDB
Hint
Use insertMany to add multiple documents to the books collection.
2
Add embedded author documents inside each book
Update the books collection to embed an author document inside each book. For "The Great Gatsby", add { name: "F. Scott Fitzgerald", birthYear: 1896 }. For "1984", add { name: "George Orwell", birthYear: 1903 }.
MongoDB
Hint
Use updateOne with $set to add the author field inside each book document.
3
Query all books with their embedded authors
Write a query to find all documents in the books collection and return the title, isbn, and embedded author fields.
MongoDB
Hint
Use find with a projection to show only the title, isbn, and author fields.
4
Complete the one-to-one embedded document pattern
Ensure the books collection documents have the embedded author field inside each book document, confirming the one-to-one embedding pattern is correctly applied.
MongoDB
Hint
Check that each book document has the author embedded document inside it.
Practice
(1/5)
1. What is the main advantage of using the one-to-one embedding pattern in MongoDB?
easy
A. It requires complex joins to retrieve related data.
B. It splits data into multiple collections to reduce document size.
C. It stores related data inside a single document for faster reads.
D. It duplicates data across collections for backup.
Solution
Step 1: Understand one-to-one embedding
One-to-one embedding stores related data inside the same document instead of separate collections.
Step 2: Identify the advantage
This approach simplifies queries and improves read speed by avoiding joins.
Final Answer:
It stores related data inside a single document for faster reads. -> Option C
Quick Check:
Embedding = faster reads [OK]
Hint: Embedding keeps related data together for quick access [OK]
Common Mistakes:
Thinking embedding splits data into multiple collections
Assuming embedding requires joins
Confusing embedding with data duplication
2. Which of the following is the correct way to embed a profile document inside a user document in MongoDB?
Hint: Embed as nested object, not separate or string [OK]
Common Mistakes:
Placing embedded document outside main document
Using array instead of object for one-to-one
Storing embedded data as string
3. Given the document { _id: 1, name: 'Bob', address: { street: 'Main St', city: 'LA' } }, what will be the result of the query db.users.findOne({ _id: 1 })?
Hint: findOne returns entire matching document including embedded [OK]
Common Mistakes:
Expecting only top-level fields returned
Thinking embedded fields are separate documents
Assuming query returns null if embedded exists
4. You have this document: { _id: 2, name: 'Carol', profile: { age: 25, city: 'Chicago' } }. Which update command correctly changes the city to 'Boston' using one-to-one embedding?
Hint: Use dot notation to update embedded fields [OK]
Common Mistakes:
Replacing whole embedded document instead of one field
Using incorrect field path without dot notation
Using invalid bracket notation in update
5. You want to design a MongoDB schema for users and their passports using one-to-one embedding. Which design is best and why?
hard
A. Embed passport details inside user document because passports are small and tightly linked.
B. Store passports in a separate collection and reference user IDs to avoid large documents.
C. Duplicate passport data in both user and passport collections for faster writes.
D. Store passport as a string field inside user document to simplify queries.
Solution
Step 1: Analyze data relationship and size
Passports are small and tightly connected to users, perfect for embedding.
Step 2: Choose embedding benefits
Embedding keeps related data together, simplifies queries, and improves read speed.
Step 3: Evaluate other options
Store passports in a separate collection and reference user IDs to avoid large documents adds complexity with references. Duplicate passport data in both user and passport collections for faster writes duplicates data causing inconsistency. Store passport as a string field inside user document to simplify queries stores data as string losing structure.
Final Answer:
Embed passport details inside user document because passports are small and tightly linked. -> Option A
Quick Check:
Small, tightly linked data = embed [OK]
Hint: Embed small, tightly linked data for simplicity and speed [OK]
Common Mistakes:
Choosing references for small one-to-one data
Duplicating data causing inconsistency
Storing structured data as plain string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.