4

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.

1

1 Answer 1

8

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
              }
            ]
          }
        ]
      }
    ]
  }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.