Mongo过滤器数组数组数组 [英] Mongo filter array of array of array

查看:83
本文介绍了Mongo过滤器数组数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过滤数组数组的列表,这是结构的一个例子.

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
                    }
                ]
            }
        ]
    }
]
}

假设我想用sampleId> 2过滤掉所有子文档

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
                    }
                ]
            }
        ]
    }
]
}

我尝试使用聚合/地图/过滤器技术,如这篇文章和其他文章中所述,但结果始终是array3空的.

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

推荐答案

您可以在下面的汇总中尝试

You can try below aggregation

基本上,您需要使用 聚合,最后使用> $filter 和最后一个.

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] }
                  }
                }
              }
            }
          }
        }
      }
    }
  }}
])

输出

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

这篇关于Mongo过滤器数组数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆