简体   繁体   English

如何使用聚合管道从嵌套的 JSON 文件中获取所有 _id?

[英]How to get all _id from the nested JSON file using aggregation pipeline?

I had the following array of structure in my aggregation pipeline.我的聚合管道中有以下结构数组。 Tried merge objects and setUnion operators.尝试合并对象和 setUnion 运算符。

{
  "_id": "6085232dd933b53b80a3de0c",
  "parent": "6082b81ea6036499a33d0972",
  "childNodes": [
    {
      "_id": "6084f8376d422a5baf527b6c",
      "childNodes": [
        {
          "_id": "6084f9ab6d422a5baf527b6d",
          "childNodes": [
            {
              "_id": "6084fa356d422a5baf527b6f",
              "childNodes": [
                {
                  "_id": "6084faa06d422a5baf527b70"
                }
              ]
            }
          ]
        },
        {
          "_id": "6084f9ab6d422a5baf527b6d",
          "childNodes": [
            {
              "_id": "6084fa356d422a5baf527b6f",
              "childNodes": [
                {
                  "_id": "6084faa06d422a5baf527b70"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

I am expecting the following results to produce some statistics.我期待以下结果产生一些统计数据。

{
 "allNodes":[allChildNodes-_ids]
}

You are asking about recursive traversal of the document tree.您正在询问文档树的递归遍历。 It's not what mongo is good at.这不是 mongo 擅长的。 Technically you can employ $function to do in javascript:从技术上讲,您可以在 javascript 中使用$function来执行:

.aggregate([
  {
    "$project": {
      _id: 0,
      "allNodes": {
        $function: {
          body: function(doc){
              allIds = function(d) {
                  return [d._id].concat(...((d.childNodes || []).map(allIds)))
              } 
              return allIds(doc)},
          args: [
            "$$ROOT"
          ],
          lang: "js"
        }
      }
    }
  }
])

but performance will suffer.但性能会受到影响。 Tail calls are not optimised either, so be careful with deeply nested documents.尾调用也没有优化,所以要小心深度嵌套的文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 nodejs中的聚合导致嵌套的json,我可以在没有嵌套的情况下得到它,只从所有collections中获取一个数据_id - aggregation in nodejs resulting in nested json, can I get it without nesting, taking only one data _id from all collections MongoDB 更新(聚合管道)使用来自嵌套嵌套 Arrays 的值 - MongoDB Update (Aggregation Pipeline) using values from nested nested Arrays 如何使用 fs 从 json 文件中获取 ID - How can I get an ID from json file using fs 使用任何lodash获取嵌套JSON对象的所有值 - Get all values of a nested JSON object using any lodash 如何使用 Mongodb 中的聚合管道将文档数组分组到 arrays 的 arrays 中? - How to group an array of documents to arrays of arrays using the aggregation pipeline in Mongodb? 如何在不使用id键的情况下“压缩”多个嵌套的JSON数组? - How to “zip” multiple nested JSON arrays without using id key? MongoDB:使用聚合/投影获取嵌套在数组中的对象的所有内容 - MongoDB : Getting all the content of an object nested in an array using aggregation/projection 如何从带有随机ID的JSON获取值 - How to get value from json with random ID 如何使用分片获取所有用户 ID? - How to get all users id using shards? 如何使用带有 ExpressJS 的 NodeJS 和 oracledb 从查询中形成和获取嵌套的 JSON 对象 - How to form and get nested JSON object from query using oracledb using NodeJS with ExpressJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM