Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

mongodb aggregate nested arrays filter not empty

But now I don’t know how to filter
I’m aggregating the filtered data:

[
       {
            "_id": "61cea071cfa3c96b9a4d2657",
            "name": "Utils",
            "children": [
                {
                    "name": "Code",
                    "_id": "61cebb4e6c4a5c643494d1a1",
                    "children": [{name:"jahn"}]
                },
                {
                    "name": "Image",
                    "_id": "61ceb8ad6c4a5c643494d11e",
                    "children": []
                }
            ]
        },
        {
            "_id": "61cea071cfa3c96b9a4d2111",
            "name": "Names",
            "children": [
               {
                    "name": "que",
                    "_id": "61cebb4e6c4a5c643494d1a1",
                    "children": [
                       
                     ]
                },
                {
                    "name": "filter",
                    "_id": "61cebb4e6c4a5c643494d1a1",
                    "children": [
                        {name:"jahn"}
                     ]
                }
            ]
        },
]

Looking forward to your help
How to filter out children when children are empty and not displayed
Desired result :

[
        {
            "_id": "61cea071cfa3c96b9a4d2657",
            "name": "Utils",
            "children": [
                {
                    "name": "Code",
                    "_id": "61cebb4e6c4a5c643494d1a1",
                    "children": [
                        {name:"jahn"}
                     ]
                }
            ]
        },
        {
            "children": [
                {
                    "children": [
                        {name:"jahn"}
                     ]
                }
            ]
        },
]

I want if children in children, if it’s empty it doesn’t show the whole object

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you tried to filter for the second level children array, you can use $filter.

db.collection.aggregate([
  {
    $project: {
      _id: 1,
      name: 1,
      children: {
        "$filter": {
          "input": "$children",
          "cond": {
            "$ne": [
              "$$this.children",
              []
            ]
          }
        }
      }
    }
  }
])

Sample Mongo Playground

Note:

"$ne": [
  "$$this.children",
  []
]

Can be replaced with:

"$ne": [
  {
    $size: "$$this.children"
  },
  0
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading