简体   繁体   中英

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.

{
  "_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. Technically you can employ $function to do in javascript:

.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM