Mongodb获取在同一表中具有父ID的记录

时间:2019-04-16 06:52:35

标签: mongodb mongoose aggregation-framework

这是我的收藏集

{ "_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" }

由于MongoDB最近引入了graphLookup,我试图使用mongooose查找查询以获取如下输出

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

0 个答案:

没有答案