Challenge - 5 Problems
Graph Lookup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find all subordinates of an employee using $graphLookup
Given a collection
employees with documents containing _id, name, and manager_id, which query correctly finds all employees under the manager with _id: 1?MongoDB
db.employees.aggregate([
{
$graphLookup: {
from: "employees",
startWith: "$_id",
connectFromField: "_id",
connectToField: "manager_id",
as: "subordinates"
}
},
{
$match: { _id: 1 }
}
])Attempts:
2 left
💡 Hint
Remember to start the graph lookup from the manager's _id and filter before the lookup.
✗ Incorrect
Option D first filters the document with _id:1, then uses $graphLookup starting from that _id to find all subordinates recursively. Other options either start from a literal 1 without filtering or use wrong fields for startWith.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in $graphLookup usage
Which option contains a syntax error in the $graphLookup stage?
MongoDB
db.collection.aggregate([
{
$graphLookup: {
from: "collection",
startWith: "$parent_id",
connectFromField: "_id",
connectToField: "parent_id",
as: "descendants"
}
}
])Attempts:
2 left
💡 Hint
Check for missing commas between fields in the object.
✗ Incorrect
Option A is missing a comma between connectToField and as fields, causing a syntax error. Other options have correct syntax except B where startWith is missing quotes but is valid if parent_id is a variable (but in MongoDB aggregation it must be a string path).
❓ optimization
advanced2:00remaining
Optimize recursive lookup to limit depth
You want to find all descendants of a node but limit recursion to 3 levels deep. Which $graphLookup option achieves this?
Attempts:
2 left
💡 Hint
maxDepth expects a number indicating maximum recursion depth.
✗ Incorrect
Option C correctly uses maxDepth: 3 to limit recursion to 3 levels. Option C uses depthField incorrectly (it names a field to store depth, not limit it). Option C uses a string instead of number for maxDepth, which is invalid. Option C sets maxDepth to 0, meaning no recursion.
🧠 Conceptual
advanced1:30remaining
Understanding $graphLookup's connectFromField and connectToField
In a $graphLookup stage, what is the role of
connectFromField and connectToField?Attempts:
2 left
💡 Hint
Think about which collection is being searched and which is the starting point.
✗ Incorrect
connectFromField is the field in the 'from' collection whose values are matched against the values in connectToField of the local collection documents. This defines how documents connect recursively.
🔧 Debug
expert2:30remaining
Why does this $graphLookup return an empty array?
Given the collection
categories with documents having _id and parent_id, the following query returns an empty children array for _id: 10. Why?
Query:
db.categories.aggregate([
{ $match: { _id: 10 } },
{ $graphLookup: {
from: "categories",
startWith: "$_id",
connectFromField: "parent_id",
connectToField: "_id",
as: "children"
}
}
])Attempts:
2 left
💡 Hint
Check which fields represent parent and child in the recursive relation.
✗ Incorrect
The query uses connectFromField: 'parent_id' and connectToField: '_id', which is backwards. To find children, connectFromField should be '_id' (child's id) and connectToField should be 'parent_id' (parent's id). This reversal causes no matches and an empty array.