Challenge - 5 Problems
MongoDB Embedding vs Referencing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
When to use embedding in MongoDB?
Which scenario is best suited for embedding documents in MongoDB?
Attempts:
2 left
💡 Hint
Think about data that is tightly connected and often retrieved in one go.
✗ Incorrect
Embedding is ideal when related data is small and accessed together, like an address inside a user document.
🧠 Conceptual
intermediate2:00remaining
When to use referencing in MongoDB?
Which situation calls for referencing documents instead of embedding in MongoDB?
Attempts:
2 left
💡 Hint
Consider data that is reused or updated separately.
✗ Incorrect
Referencing is preferred when related data is large or shared across many documents to avoid duplication.
❓ query_result
advanced3:00remaining
Output of embedding vs referencing query
Given two collections, users with embedded addresses and orders referencing users, what is the output of this query?
MongoDB
db.orders.aggregate([
{ $lookup: {
from: 'users',
localField: 'user_id',
foreignField: '_id',
as: 'user_info'
}},
{ $unwind: '$user_info' },
{ $project: {
order_id: 1,
'user_info.name': 1,
'user_info.address.city': 1
}}
])Attempts:
2 left
💡 Hint
Look at the $project stage selecting name and address.city fields.
✗ Incorrect
The $lookup joins orders with users by user_id. The $project includes order_id, user name, and city from embedded address.
❓ schema
advanced3:00remaining
Choose the best schema design for a blog with comments
You are designing a MongoDB schema for a blog where each post can have hundreds of comments. Which design is best?
Attempts:
2 left
💡 Hint
Consider document size limits and update frequency.
✗ Incorrect
Embedding hundreds of comments can exceed document size limits and cause performance issues. Referencing comments separately is better.
❓ optimization
expert4:00remaining
Optimizing queries with embedding vs referencing
You have a MongoDB collection of products and a collection of reviews referencing products. You want to optimize read performance for showing product details with reviews. Which approach is best?
Attempts:
2 left
💡 Hint
Balance document size and query speed for common use cases.
✗ Incorrect
Embedding only recent reviews keeps documents manageable and speeds up common queries, while older reviews remain accessible separately.