What is the MongoDB Aggregation Framework's $graphLookup stage?
Why Interviewers Ask This
Advanced questions like this reveal whether a candidate has internalized MongoDB deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.
Answer
The $graphLookup stage performs a recursive traversal of a collection — like a recursive SQL query or graph traversal. It recursively looks up documents that match a condition, starting from a root document and following a chain of connected documents. Syntax: { $graphLookup: { from: "employees", startWith: "$reportsTo", connectFromField: "reportsTo", connectToField: "_id", as: "reportingChain", maxDepth: 10, depthField: "hierarchyLevel", restrictSearchWithMatch: { department: "$department" } } }. Parameters: from: collection to search; startWith: expression computing the initial set of values; connectFromField: field in the "from" collection to follow recursively; connectToField: field in the "from" collection to match against; as: output array field name; maxDepth: maximum recursion depth (required to prevent infinite loops in cyclic graphs); depthField: optional field to add to each result document indicating its depth; restrictSearchWithMatch: additional filter conditions on each recursive step. Use cases: (1) Organizational hierarchy: find all reports under a manager (recursive); (2) Category trees: find all subcategories of a category; (3) Friend networks: find all friends-of-friends within N degrees; (4) Bill of materials: components of components; (5) Thread replies: nested comment trees. Performance: $graphLookup can be expensive on large collections — index the connectToField for efficient lookups at each depth level.
Pro Tip
This topic has MongoDB-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.
More MongoDB Questions
View all →- Advanced How does MongoDB replication work internally?
- Advanced How does MongoDB sharding distribute data internally?
- Advanced What is the WiredTiger cache and how does it affect performance?
- Advanced What is the Aggregation $setWindowFields stage?
- Advanced How does MongoDB handle index builds on large collections?