Mongodb mongo聚合如何选择‘$graphLookup或$lookup’;

Mongodb mongo聚合如何选择‘$graphLookup或$lookup’;,mongodb,mongoose,Mongodb,Mongoose,我不知道如何选择“$graphLookup或$lookup”其他类似的选项 我期待mongodb正式提供更完整的文档 例如: {parentId: 0, cid: 1, other: 'a'}, {parentId: 0, cid: 2, other: 'b'}, {parentId: 0, cid: 3, other: 'c'}, {parentId: 1, cid: 11, other: 'aa'}, {parentId: 2, cid: 12, other: 'ab'}, {parent

我不知道如何选择“$graphLookup或$lookup”其他类似的选项

我期待mongodb正式提供更完整的文档

例如:

{parentId: 0, cid: 1, other: 'a'},
{parentId: 0, cid: 2, other: 'b'},
{parentId: 0, cid: 3, other: 'c'},

{parentId: 1, cid: 11, other: 'aa'},
{parentId: 2, cid: 12, other: 'ab'},
{parentId: 3, cid: 13, other: 'ac'},
结果:

{
parentId: 0, cid: 1, other: 'a', 
  children: [
   {parentId: 1, cid: 11, other: 'aa'},
  ]
},{
parentId: 0, cid: 2, other: 'b',
  children: [
   {parentId: 2, cid: 12, other: 'ab'},
  ]
},{
parentId: 0, cid: 3, other: 'c',
  children: [
   {parentId: 3, cid: 13, other: 'ac'},
  ]
}
},

怎么做?

您需要使用


用于“加入”集合以进行处理。

您这样做的结果是``children:[]``谢谢您的回答
db.collection.aggregate([
  {
    $match: {
      "parentId": {
        $eq: 0
      }
    }
  },
  {
    $graphLookup: {
      from: "collection",
      startWith: "$cid",
      connectFromField: "cid",
      connectToField: "parentId",
      as: "children"
    }
  }
])