猫鼬:按嵌套数组排序

时间:2019-04-22 09:56:49

标签: mongodb mongoose aggregate lookup

名为雇员的集合具有以下文档:

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

以下$ graphLookup操作与employees集合中的reportTo和name字段递归匹配,返回每个人的报告层次结构:

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

如何根据级别字段(depthField)值对reportingHierarchy进行排序?

0 个答案:

没有答案