mongo 聚合如何选择“$graphLookup 或 $lookup”

链接

我不知道如何选择 '$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'},
  ]
}
},

怎么办?

阿肖克

你需要使用$graphLookup

db.collection.aggregate([
  {
    $match: {
      "parentId": {
        $eq: 0
      }
    }
  },
  {
    $graphLookup: {
      from: "collection",
      startWith: "$cid",
      connectFromField: "cid",
      connectToField: "parentId",
      as: "children"
    }
  }
])

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

mongo 聚合 $lookup 与数组

$ lookup数组mongo聚合中的每个元素

Mongo Query:如何使用DBRef进行$ lookup

使用不匹配值的$ lookup的Mongo聚合对象

Mongo聚合和带有$ lookup的$ group嵌套数组

如何在mongo db中计算$ lookup字段?

如何在mongo聚合和Lookup ...中将“类型”信息复制到“实例”中并保留原始列表?

使用let时,带有$ lookup的Mongo聚合不起作用

Mongo DB聚合$ lookup缓慢将索引添加到所有文档吗?

如何限制从$ lookup操作返回的字段添加到Mongo视图

如果Mongo $ lookup是左外部联接,那么它将如何排除不匹配的文档呢?

Spring数据mongo中的MongoDB $ Lookup

Mongo Lookup使用$ id参考加入集合

Mongo多个$ lookup和$ group合计

在$ lookup MONGO上提高$ match的速度

Mongo任何集合中有多个$ lookup

如何获得Mongo聚合的分页

MongoDB $ lookup或$ graphLookup递归聚合,并在有条件的情况下停止查找

MongoDB聚合查询$ lookup

聚合$ lookup对象数组

$ lookup聚合的多个条件

MongoDB聚合$ lookup $ match

$ lookup在聚合中可以在mongo shell命令中正常工作,但是在尝试使用mongoose节点进行尝试时,然后获取了空数组作为响应

Mongo $ lookup具有更多集合和空字段

具有多个$ match或$ pipeline条件的Mongo $ lookup

使用嵌套查询的Mongo $ lookup过滤器

Mongo $ lookup使用mongoose嵌套3级数组

mongo管道中的$ in / $ eq $ lookup不起作用

具有多个$ expr参数的Mongo $ lookup $ pipeline在$ lookup结果中未返回正确的数据