Challenge - 5 Problems
MongoDB $lookup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this $lookup aggregation?
Given two collections, orders and customers, what will be the result of this aggregation on orders?
MongoDB
db.orders.aggregate([
{
$lookup: {
from: "customers",
localField: "customerId",
foreignField: "_id",
as: "customer_info"
}
}
])Attempts:
2 left
💡 Hint
Think about which collection the aggregation runs on and what $lookup does.
✗ Incorrect
The $lookup stage adds a new array field to each input document (orders) containing matching documents from the 'from' collection (customers).
📝 Syntax
intermediate2:00remaining
Which $lookup syntax is correct?
Identify the valid $lookup stage syntax to join 'products' with 'categories' on 'categoryId' and '_id'.
Attempts:
2 left
💡 Hint
localField is from the current collection, foreignField is from the joined collection.
✗ Incorrect
Option A correctly matches 'categoryId' in products to '_id' in categories and specifies 'as' field.
❓ optimization
advanced2:00remaining
How to optimize $lookup for large collections?
You have two large collections: 'sales' and 'products'. Which approach optimizes $lookup performance best?
Attempts:
2 left
💡 Hint
Filtering early reduces data processed in join.
✗ Incorrect
Using $lookup with a pipeline and $match filters the joined collection before joining, improving performance on large datasets.
🔧 Debug
advanced2:00remaining
Why does this $lookup return empty arrays?
Given these collections, why does this aggregation return empty arrays in 'joined_docs'?
MongoDB
db.collection1.aggregate([
{
$lookup: {
from: "collection2",
localField: "id",
foreignField: "_id",
as: "joined_docs"
}
}
])Attempts:
2 left
💡 Hint
Check if the fields used for joining have the same type.
✗ Incorrect
If the join fields have different types (e.g., string vs ObjectId), no matches occur, resulting in empty arrays.
🧠 Conceptual
expert2:00remaining
What is the main limitation of $lookup in MongoDB?
Which statement best describes a key limitation of $lookup when joining collections?
Attempts:
2 left
💡 Hint
Think about database boundaries and $lookup capabilities.
✗ Incorrect
$lookup can only join collections within the same database; cross-database joins are not supported.