Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why Document Design Matters in MongoDB
📖 Scenario: You are working for a small online bookstore. You want to store information about books and their authors in a MongoDB database. Good document design will help you store and retrieve data efficiently.
🎯 Goal: Build a MongoDB collection with well-designed documents that store book titles, authors, and publication years. Learn how to structure documents for easy querying and updating.
📋 What You'll Learn
Create a collection called books with documents containing title, author, and year fields.
Add a configuration variable maxYear to filter books published after this year.
Write a query to find all books published after maxYear.
Add an index on the year field to improve query performance.
💡 Why This Matters
🌍 Real World
Good document design helps online stores and apps store and retrieve data quickly and clearly.
💼 Career
Database developers and data engineers must design documents and indexes to optimize performance and maintainability.
Progress0 / 4 steps
1
Create the books collection with initial documents
Create a MongoDB collection called books and insert these exact documents: { title: "The Great Gatsby", author: "F. Scott Fitzgerald", year: 1925 }, { title: "1984", author: "George Orwell", year: 1949 }, and { title: "To Kill a Mockingbird", author: "Harper Lee", year: 1960 }.
MongoDB
Hint
Use insertMany to add multiple documents to the books collection.
2
Add a configuration variable maxYear
Create a variable called maxYear and set it to 1930. This will be used to filter books published after this year.
MongoDB
Hint
Use const maxYear = 1930 to create the variable.
3
Query books published after maxYear
Write a MongoDB query using find on the books collection to get all documents where the year field is greater than maxYear. Store the query in a variable called recentBooks.
MongoDB
Hint
Use db.books.find({ year: { $gt: maxYear } }) to get books published after maxYear.
4
Add an index on the year field
Create an index on the year field of the books collection using createIndex to improve query speed.
MongoDB
Hint
Use db.books.createIndex({ year: 1 }) to create an ascending index on the year field.
Practice
(1/5)
1. Why is good document design important in MongoDB?
easy
A. It groups related data together for faster access.
B. It makes the database use more disk space.
C. It forces all data to be stored in separate collections.
D. It prevents any data from being updated.
Solution
Step 1: Understand document design purpose
Good document design groups related data to reduce the number of database lookups.
Step 2: Identify the benefit of grouping data
Grouping related data together makes data access faster and simpler for the application.
Final Answer:
It groups related data together for faster access. -> Option A
Quick Check:
Good design = grouped data = faster access [OK]
Hint: Good design groups related data for speed [OK]
Common Mistakes:
Thinking design increases disk space unnecessarily
Believing all data must be in separate collections
Assuming design stops data updates
2. Which of the following is the correct way to embed an address inside a user document in MongoDB?
Hint: Embed data as nested objects, not arrays or strings [OK]
Common Mistakes:
Using arrays instead of objects for embedded data
Storing address as a plain string
Leaving embedded fields null without reason
3. Given this user document: { _id: 1, name: 'Bob', orders: [{ id: 101, total: 50 }, { id: 102, total: 30 }] } What will be the result of the query db.users.findOne({ _id: 1 })?
Thinking query returns null if embedded arrays exist
Confusing syntax errors with valid queries
4. You want to embed a list of comments inside a blog post document, but your code throws an error. Which is the likely cause? { title: 'Post', comments: 'Great post!' }
medium
A. Comments should be an array of objects, not a string.
B. Title field cannot be a string.
C. MongoDB does not allow embedding arrays.
D. The document must have an _id field.
Solution
Step 1: Check the comments field type
Comments are given as a string, but embedding multiple comments requires an array of objects.
Step 2: Understand embedding requirements
Embedding multiple related items means using an array of objects, not a single string.
Final Answer:
Comments should be an array of objects, not a string. -> Option A
Quick Check:
Embed lists as arrays, not strings [OK]
Hint: Embed lists as arrays of objects, not strings [OK]
Common Mistakes:
Using string instead of array for multiple items
Thinking title cannot be string
Believing MongoDB forbids arrays
Assuming _id is always required manually
5. You have a product catalog where each product has many reviews. Reviews can grow large over time. What is the best document design to handle this efficiently?
hard
A. Embed all reviews inside each product document.
B. Duplicate product data inside each review document.
C. Store only the first review inside the product document.
D. Store reviews in a separate collection linked by product ID.
Solution
Step 1: Consider document size limits and growth
Embedding many reviews inside a product can make the document very large and slow to update.
Step 2: Choose design for large growing data
Storing reviews separately and linking by product ID keeps product documents small and queries efficient.
Final Answer:
Store reviews in a separate collection linked by product ID. -> Option D
Quick Check:
Large growing data = separate collection [OK]
Hint: Large growing lists? Use separate collections [OK]