What is the purpose of the $graphLookup stage in MongoDB?
The $graphLookup stage is used to perform recursive searches on documents within a collection, allowing you to find related data connected through a chain of references.
Click to reveal answer
beginner
In $graphLookup, what does the 'connectFromField' specify?
The 'connectFromField' specifies the field in the documents where the recursion starts and continues by matching values to the 'connectToField' in other documents.
Click to reveal answer
intermediate
How does the 'maxDepth' option affect a $graphLookup operation?
The 'maxDepth' option limits how many levels deep the recursive search will go, preventing infinite loops and controlling the size of the result.
Click to reveal answer
beginner
What is the role of the 'as' field in a $graphLookup stage?
The 'as' field defines the name of the new array field added to each document, which contains the results of the recursive lookup.
Click to reveal answer
intermediate
Why is $graphLookup useful for hierarchical data like organizational charts or category trees?
$graphLookup helps by automatically traversing parent-child relationships recursively, making it easy to retrieve all connected nodes without multiple queries.
Click to reveal answer
Which MongoDB aggregation stage allows recursive lookup within the same collection?
A$graphLookup
B$lookup
C$match
D$group
✗ Incorrect
$graphLookup is designed for recursive searches within a collection, unlike $lookup which joins different collections.
In $graphLookup, what does 'connectToField' represent?
AField to match against 'connectFromField' in other documents
BField to store results
CField to limit recursion depth
DField to filter documents
✗ Incorrect
'connectToField' is the field in other documents that 'connectFromField' values are matched to during recursion.
What happens if you omit 'maxDepth' in a $graphLookup?
AAn error occurs
BRecursion stops immediately
COnly one level is searched
DRecursion continues until no more matches are found
✗ Incorrect
Without 'maxDepth', $graphLookup will recurse until no further matches exist, which can be deep or infinite.
Which option in $graphLookup stores the recursive results in the output documents?
AstartWith
BmaxDepth
Cas
DdepthField
✗ Incorrect
The 'as' option names the new array field that holds the recursive lookup results.
What type of data is best suited for $graphLookup?
AFlat, unrelated data
BHierarchical or graph-structured data
CSimple key-value pairs
DUnstructured text
✗ Incorrect
$graphLookup is ideal for hierarchical or graph data where recursive relationships exist.
Explain how $graphLookup works to find recursive relationships in MongoDB data.
Think about how you follow links from one document to another repeatedly.
You got /4 concepts.
Describe a real-life example where $graphLookup would be useful and why.
Imagine tracing all managers above an employee or all subcategories under a category.
You got /3 concepts.
Practice
(1/5)
1. What is the primary purpose of the $graphLookup stage in MongoDB aggregation?
easy
A. To create indexes on fields
B. To perform a simple filter on documents
C. To update multiple documents at once
D. To recursively search connected documents within the same collection
Solution
Step 1: Understand the role of $graphLookup
$graphLookup is designed to recursively find related documents by following references within the same collection, useful for hierarchical data.
Step 2: Compare with other options
Options B, C, and D describe other MongoDB operations unrelated to recursive lookup.
Final Answer:
To recursively search connected documents within the same collection -> Option D
Quick Check:
$graphLookup = recursive search [OK]
Hint: Remember $graphLookup is for recursive connected data search [OK]
Common Mistakes:
Confusing $graphLookup with $match filtering
Thinking $graphLookup updates documents
Assuming $graphLookup creates indexes
2. Which of the following is the correct syntax to start a $graphLookup stage in a MongoDB aggregation pipeline?
In $graphLookup, startWith uses the field to start recursion, usually a reference like "$managerId". The connectToField is the field in the foreign documents to match against the start value, here "_id". The connectFromField is the field in the foreign document used for the next recursion, here "managerId".
Step 2: Match syntax with options
{"$graphLookup": {"from": "employees", "startWith": "$managerId", "connectFromField": "managerId", "connectToField": "_id", "as": "reportingHierarchy"}} correctly uses startWith: "$managerId", connectFromField: "managerId", and connectToField: "_id". Others swap these or use incorrect field names.
A. An array 'ancestors' containing documents with _id 2 and 1
B. An empty array 'ancestors'
C. An array 'ancestors' containing only document with _id 1
D. A syntax error due to incorrect $graphLookup usage
Solution
Step 1: Understand the recursive lookup
Starting from the document with _id 3, startWith uses its parent field which is 2. Then it finds documents where _id matches 2, then recursively finds parents of 2, which is 1.
Step 2: Trace the ancestors found
The recursion finds documents with _id 2 and then _id 1, so the 'ancestors' array contains both documents.
Final Answer:
An array 'ancestors' containing documents with _id 2 and 1 -> Option A
Quick Check:
Recursive parents = [2, 1] [OK]
Hint: Trace startWith field value and follow connectToField recursively [OK]
5. You have a collection employees where each document has _id and managerId. You want to find all employees under a manager recursively, including the manager. Which $graphLookup configuration correctly achieves this?
To find all employees under a manager, start with the manager's _id. Then find employees whose managerId matches this _id, recursively.
Step 2: Match fields in $graphLookup
startWith should be $_id (the manager's id). connectToField is managerId (field in employees to match), and connectFromField is _id (field to match against for next level).
Step 3: Verify options
{"$graphLookup": {"from": "employees", "startWith": "$_id", "connectFromField": "_id", "connectToField": "managerId", "as": "team"}} matches this logic exactly. {"$graphLookup": {"from": "employees", "startWith": "$_id", "connectFromField": "managerId", "connectToField": "_id", "as": "team"}} swaps connectFromField and connectToField incorrectly. The options starting with $managerId are wrong for this use case.