名为雇员的集合具有以下文档:
{ "_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
进行排序?