$graphLookup

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Pipeline stages

What it does

Recursively traverses relationships.

Syntax

{$graphLookup: { from, startWith, connectFromField, connectToField, as, maxDepth? }}

Use case

Retrieve ancestors/descendants.

Important notes

Use depth limits and index relationship fields.

$graphLookup: practical example
{ $graphLookup: { from: "categories", startWith: "$parentId", connectFromField: "parentId", connectToField: "_id", as: "ancestors", maxDepth: 5 } }
💡 Explanation Recursively traverses relationships. When to use it Retrieve ancestors/descendants. Caution Use depth limits and index relationship fields.
Key Takeaways
  • Recursively traverses relationships.
  • Typical use: Retrieve ancestors/descendants.