具有固定起点的Mongodb图形查找

时间:2017-10-17 18:09:04

标签: mongodb mongodb-query aggregation-framework

我正在使用MongoDB 3.4。以下是集合的结构

{ "_id" : 1, "name" : "Dev" }
{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" }
{ "_id" : 3, "name" : "Ron", "reportsTo" : "Eliot" }
{ "_id" : 4, "name" : "Andrew", "reportsTo" : "Eliot" }
{ "_id" : 5, "name" : "Asya", "reportsTo" : "Ron" }
{ "_id" : 6, "name" : "Dan", "reportsTo" : "Andrew" }

我可以使用以下查询来查找所有层次结构

db.employees.aggregate( [
   {
      $graphLookup: {
         from: "employees",
         startWith: "$reportsTo",
         connectFromField: "reportsTo",
         connectToField: "name",
         as: "reportingHierarchy"
      }
   }
] )

但是如何只找到一个员工的层次结构,例如阿霞或丹。什么是查询?

0 个答案:

没有答案