小编ric*_*ian的帖子

Mongodb $graphLookup 构建层次结构

我有一个来自 mongodb$graphLookup聚合的输出:

db.getCollection('projects').aggregate([
    {
    $lookup: {
      from: "projects",
      localField: "_id",
      foreignField: "parent",
       as: "childrens"
     }
 }
])
Run Code Online (Sandbox Code Playgroud)
{
    "_id" : "1",
    "name" : "Project1",
    "parent" : null,
    "childrens" : [ 
        {
            "_id" : "3",
            "name" : "ProjectForId1",
            "parent" : "1"
        }
    ]
},
{
    "_id" : "3",
    "name" : "ProjectForId1",
    "parent" : "1",
    "childrens" : [ 
        {
            "_id" : "6",
            "name" : "ProjectForId3",
            "parent" : "3"
        }, 
        {
            "_id" : "7",
            "name" : "ProjectForId3",
            "parent" : "3"
        } …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose mongodb graphlookup

1
推荐指数
1
解决办法
1497
查看次数

标签 统计

graphlookup ×1

javascript ×1

mongodb ×1

mongoose ×1