0
0
MongoDBquery~10 mins

Graph lookup for recursive data in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Graph lookup for recursive data
Start with root node
Lookup connected nodes
Add connected nodes to results
For each connected node
Repeat lookup recursively
Stop when no new nodes found
Return full recursive graph
Start from a root node, find connected nodes recursively, and collect all related nodes until no new nodes are found.
Execution Sample
MongoDB
db.nodes.aggregate([
  {
    $graphLookup: {
      from: "nodes",
      startWith: "$_id",
      connectFromField: "parent",
      connectToField: "_id",
      as: "ancestors"
    }
  }
])
This query recursively finds all ancestor nodes starting from the '_id' field of each document.
Execution Table
StepActionCurrent Node _idConnected Nodes FoundAccumulated Results
1Start with root node5parent: 3[5]
2Lookup connected nodes3parent: 1[5, 3]
3Lookup connected nodes1parent: null[5, 3, 1]
4No more connected nodesnullnone[5, 3, 1]
5Return full recursive graphnullnone[5, 3, 1]
💡 No more connected nodes found, recursion ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Current Node _idnull531null
Connected Nodes Found[][3][1][][]
Accumulated Results[][5][5, 3][5, 3, 1][5, 3, 1]
Key Moments - 2 Insights
Why does the recursion stop when the connected node is null?
Because in the execution_table at Step 3 and 4, no new connected nodes are found (parent is null), so the recursion ends.
How does $graphLookup avoid infinite loops in recursive data?
It tracks accumulated results (see Accumulated Results column) and stops when no new nodes are found, preventing revisiting the same nodes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Accumulated Results after Step 2?
A[3]
B[5, 3]
C[5]
D[5, 3, 1]
💡 Hint
Check the 'Accumulated Results' column in the execution_table row for Step 2.
At which step does the recursion find no more connected nodes?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Connected Nodes Found' column in the execution_table to find where it becomes 'none'.
If the root node had no parent, what would be the Accumulated Results after Step 1?
A[root node id]
B[parent id]
C[]
Dnull
💡 Hint
Refer to variable_tracker for how Accumulated Results starts with the root node id.
Concept Snapshot
Use $graphLookup to recursively find connected documents.
Start with a field (startWith).
Connect from one field to another (connectFromField, connectToField).
Collect all connected nodes in an array (as).
Stops when no new nodes are found to avoid infinite loops.
Full Transcript
This visual execution shows how MongoDB's $graphLookup works to find recursive data. Starting from a root node, it looks up connected nodes by matching fields. Each connected node is added to the results. The process repeats recursively for each connected node until no new nodes are found. The accumulated results grow step by step, and recursion stops when no more connections exist. This prevents infinite loops and collects the full recursive graph data.