📖 Scenario: You are working for a small online bookstore. You want to store information about books and their authors in a MongoDB database. How you decide to organize this data affects how easy it is to find and update information later.
🎯 Goal: Build a simple MongoDB collection that shows two different ways to model books and authors: embedding author details inside each book document, and referencing authors in a separate collection. This will help you understand why modeling decisions matter.
📋 What You'll Learn
Create a books collection with embedded author details.
Create an authors collection with author documents.
Create a books_ref collection where each book references an author by author_id.
Write queries to find books with embedded authors and books with referenced authors.
💡 Why This Matters
🌍 Real World
Choosing how to model data in MongoDB is important for building efficient and maintainable applications like online stores, blogs, or social networks.
💼 Career
Understanding data modeling helps database developers and backend engineers design databases that perform well and are easy to work with.
Progress0 / 4 steps
1
Create the books collection with embedded author details
Create a MongoDB collection called books and insert two documents. Each document should have the fields title and author. The author field should be an embedded document with name and birth_year. Use these exact entries: { title: "The Great Gatsby", author: { name: "F. Scott Fitzgerald", birth_year: 1896 } } and { title: "1984", author: { name: "George Orwell", birth_year: 1903 } }.
MongoDB
Hint
Use db.books.insertMany() with an array of documents. Each document should have title and an embedded author document.
2
Create the authors collection with separate author documents
Create a MongoDB collection called authors and insert two documents. Each document should have the fields _id, name, and birth_year. Use these exact entries: { _id: 1, name: "F. Scott Fitzgerald", birth_year: 1896 } and { _id: 2, name: "George Orwell", birth_year: 1903 }.
MongoDB
Hint
Use db.authors.insertMany() with an array of author documents. Each must have an _id field.
3
Create the books_ref collection with author references
Create a MongoDB collection called books_ref and insert two documents. Each document should have the fields title and author_id. Use these exact entries: { title: "The Great Gatsby", author_id: 1 } and { title: "1984", author_id: 2 }.
MongoDB
Hint
Use db.books_ref.insertMany() with documents that have title and author_id fields.
4
Query books with embedded authors and referenced authors
Write two queries: one to find all books in the books collection showing embedded author details, and one to find all books in the books_ref collection joined with author details from the authors collection using $lookup. Assign the results to variables embeddedBooks and referencedBooks respectively.
MongoDB
Hint
Use db.books.find().toArray() to get embedded books. Use db.books_ref.aggregate() with $lookup and $unwind to join authors.
Practice
(1/5)
1. Why is choosing the right data model important in MongoDB?
easy
A. It decides the font used in the database shell
B. It changes the color of the database interface
C. It affects how fast and easy your app can access data
D. It controls the network speed of your internet
Solution
Step 1: Understand data model impact
The data model determines how data is stored and accessed, affecting speed and ease of use.
Step 2: Eliminate unrelated options
Options about colors, fonts, or internet speed do not relate to data modeling.
Final Answer:
It affects how fast and easy your app can access data -> Option C
Quick Check:
Data model affects app speed and ease [OK]
Hint: Data model = app speed and ease [OK]
Common Mistakes:
Confusing UI features with data modeling
Thinking data model affects internet speed
Ignoring performance impact of modeling
2. Which of the following is the correct way to embed an address inside a user document in MongoDB?
Hint: Query embedded fields with dot notation [OK]
Common Mistakes:
Assuming query matches documents without 'Book'
Expecting syntax error from dot notation
Confusing array matching with string matching
4. You have a user document with an embedded array of comments. You try to update a comment's text but the update does not work. Which is the likely error?
D. Missing $ positional operator to target the correct array element
Solution
Step 1: Analyze update query
The filter matches the comment with id 5, but the update sets 'comments.text' without specifying which array element.
Step 2: Identify missing positional operator
To update a specific array element, the $ positional operator is needed in the update path: 'comments.$.text'.
Final Answer:
Missing $ positional operator to target the correct array element -> Option D
Quick Check:
Use $ to update matched array element [OK]
Hint: Use $ to update specific array item [OK]
Common Mistakes:
Forgetting $ in update path for arrays
Changing updateOne to updateMany unnecessarily
Assuming collection name or filter syntax is wrong
5. You want to model a blog app where each post has many comments. Comments can be very large and users often read posts without comments. Which modeling choice is best and why?
hard
A. Embed comments inside posts to get all data in one query
B. Store comments in a separate collection and reference posts to avoid large post documents
C. Store comments as strings inside posts to save space
D. Duplicate comments in both posts and a separate collection
Solution
Step 1: Analyze data size and access pattern
Comments are large and often not needed when reading posts, so embedding would make posts heavy.
Step 2: Choose referencing for efficiency
Referencing comments separately keeps post documents small and loads comments only when needed.
Final Answer:
Store comments in a separate collection and reference posts to avoid large post documents -> Option B
Quick Check:
Large optional data = use referencing [OK]
Hint: Large optional data? Use referencing [OK]
Common Mistakes:
Embedding large optional data causing slow reads
Storing comments as strings losing structure
Duplicating data causing inconsistency
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.