标签: graphlookup

每次执行查询时聚合 $graphLookup 都会以不同的顺序检索结果?

我有以下数据集(类别):

[{
  "_id": 1,
  "name": "Root",
  "parent": null
},
 {
   "_id": 2,
   "name": "Sub - Level 1",
   "parent": 1
 }, {
   "_id": 3,
   "name": "Sub - Level 2",
   "parent": 2
 }, {
   "_id": 4,
   "name": "Sub - Level 3",
   "parent": 3
 }
]
Run Code Online (Sandbox Code Playgroud)

我正在该数据集上运行以下管道以递归方式获取树:

[{
  '$match': {
    '_id': 1
  }
}, {
  '$graphLookup': {
    'from': 'categories', 
    'startWith': '$_id', 
    'connectFromField': '_id', 
    'connectToField': 'parent', 
    'as': 'menu'
  }
}, {
    '$sort': {
      'menu.parent': 1
    }
  }]
Run Code Online (Sandbox Code Playgroud)

目的是像这样递归地获取树:

{
    "_id": 1,
    "name": "Root",
    "parent: …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb mongodb-query aggregation-framework graphlookup

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

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
查看次数