0
0
MongoDBquery~20 mins

Graph lookup for recursive data in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Graph Lookup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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 }
  }
])
A
db.employees.aggregate([
  {
    $graphLookup: {
      from: "employees",
      startWith: "manager_id",
      connectFromField: "_id",
      connectToField: "manager_id",
      as: "subordinates"
    }
  },
  {
    $match: { _id: 1 }
  }
])
B
db.employees.aggregate([
  {
    $graphLookup: {
      from: "employees",
      startWith: 1,
      connectFromField: "_id",
      connectToField: "manager_id",
      as: "subordinates"
    }
  }
])
C
db.employees.aggregate([
  {
    $match: { _id: 1 }
  },
  {
    $graphLookup: {
      from: "employees",
      startWith: "$manager_id",
      connectFromField: "_id",
      connectToField: "manager_id",
      as: "subordinates"
    }
  }
])
D
db.employees.aggregate([
  {
    $match: { _id: 1 }
  },
  {
    $graphLookup: {
      from: "employees",
      startWith: "$_id",
      connectFromField: "_id",
      connectToField: "manager_id",
      as: "subordinates"
    }
  }
])
Attempts:
2 left
💡 Hint
Remember to start the graph lookup from the manager's _id and filter before the lookup.
📝 Syntax
intermediate
1: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"
    }
  }
])
A{ $graphLookup: { from: "collection", startWith: "$parent_id", connectFromField: "_id", connectToField: "parent_id", as: "descendants" } }
B{ $graphLookup: { from: "collection", startWith: parent_id, connectFromField: "_id", connectToField: "parent_id", as: "descendants" } }
C $graphLookup: { from: "collection", startWith: "$parent_id", connectFromField: "_id", connectToField: "parent_id", as: "descendants" } }
D} } "stnadnecsed" :sa ,"di_tnerap" :dleiFoTtcennoc ,"di_" :dleiFmorFtcennoc ,"di_tnerap$" :htiWtrats ,"noitcelloc" :morf { :pukooLhparg$ {
Attempts:
2 left
💡 Hint
Check for missing commas between fields in the object.
optimization
advanced
2: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?
A{ $graphLookup: { from: "nodes", startWith: "$_id", connectFromField: "_id", connectToField: "parent_id", as: "descendants", maxDepth: "3" } }
B{ $graphLookup: { from: "nodes", startWith: "$_id", connectFromField: "_id", connectToField: "parent_id", as: "descendants", depthField: 3 } }
C{ $graphLookup: { from: "nodes", startWith: "$_id", connectFromField: "_id", connectToField: "parent_id", as: "descendants", maxDepth: 3 } }
D{ $graphLookup: { from: "nodes", startWith: "$_id", connectFromField: "_id", connectToField: "parent_id", as: "descendants", maxDepth: 0 } }
Attempts:
2 left
💡 Hint
maxDepth expects a number indicating maximum recursion depth.
🧠 Conceptual
advanced
1:30remaining
Understanding $graphLookup's connectFromField and connectToField
In a $graphLookup stage, what is the role of connectFromField and connectToField?
AconnectFromField is the field in the 'from' collection to match, connectToField is the field in the local collection to match against.
BconnectFromField is the field in the local collection to match, connectToField is the field in the 'from' collection to match against.
CconnectFromField is the field in the 'from' collection to match against, connectToField is the field in the 'from' collection to match.
DconnectFromField and connectToField are both fields in the local collection used for matching.
Attempts:
2 left
💡 Hint
Think about which collection is being searched and which is the starting point.
🔧 Debug
expert
2: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"
    }
  }
])
AThe 'as' field name 'children' is reserved and cannot be used.
BconnectFromField and connectToField are reversed; connectFromField should be '_id' and connectToField 'parent_id'.
CThe $match stage should come after $graphLookup, not before.
DstartWith should be a literal value 10, not '$_id'.
Attempts:
2 left
💡 Hint
Check which fields represent parent and child in the recursive relation.