Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo filter array of array of array

I'm trying to filter the list of an array of array arrays, this is an example of structure.

{
    "array1": [
    {
        "array2": [
            {
                "array3": [
                    {
                        "sampleId": 1
                    },
                    {
                        "sampleId": 2
                    },
                    {
                        "sampleId": 5
                    }
                ]
            },
            {
                "array3": [
                    {
                        "sampleId": 7
                    },
                    {
                        "sampleId": 8
                    }
                ]
            }
        ]
    },
    {
        "array2": [
            {
                "array3": [
                    {
                        "sampleId": 1
                    }
                ]
            }
        ]
    }
]
}

Let's say that i want to filter out all the subdocuments with sampleId > 2

this is an example of the expected result.

{
"array1": [
    {
        "array2": [
            {
                "array3": [
                    {
                        "sampleId": 1
                    },
                    {
                        "sampleId": 2
                    }
                ]
            },
            {
                "array3": []
            }
        ]
    },
    {
        "array2": [
            {
                "array3": [
                    {
                        "sampleId": 1
                    }
                ]
            }
        ]
    }
]
}

I tried using aggregation/map/filter technique as explained in this post and others but the results are always giving array3 empty.

like image 598
Oxenarf Avatar asked Oct 18 '25 10:10

Oxenarf


1 Answers

You can try below aggregation

Basically you need to loop over each array using $map aggregation and finally use $filter with the last one.

db.collection.aggregate([
  { "$project": {
    "array1": {
      "$map": {
        "input": "$array1",
        "as": "a1",
        "in": {
          "array2": {
            "$map": {
              "input": "$$a1.array2",
              "as": "a2",
              "in": {
                "array3": {
                  "$filter": {
                    "input": "$$a2.array3",
                    "as": "a3",
                    "cond": { "$lte": ["$$a3.sampleId", 2] }
                  }
                }
              }
            }
          }
        }
      }
    }
  }}
])

Output

[
  {
    "array1": [
      {
        "array2": [
          {
            "array3": [
              {
                "sampleId": 1
              },
              {
                "sampleId": 2
              }
            ]
          },
          {
            "array3": []
          }
        ]
      },
      {
        "array2": [
          {
            "array3": [
              {
                "sampleId": 1
              }
            ]
          }
        ]
      }
    ]
  }
]
like image 60
Ashh Avatar answered Oct 20 '25 15:10

Ashh