$filter 在 mongodb 中最多嵌套 3 个级别 [英] $filter upto 3 nested level in mongodb

查看:22
本文介绍了$filter 在 mongodb 中最多嵌套 3 个级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下收藏

<预><代码>[{数组1":[{阵列2":[{"name": "6666",阵列3":[{_id":128938120,巢":三星"},{_id":12803918239,巢":诺基亚"}]},{"name": "5555",阵列3":[{ "_id": 48102938109, "nest": "iphone" },{ "_id": 501293890, "nest": "micromax" }]}],"name": "旧公寓"},{阵列2":[{_id":410923810,"name": "3333",阵列3":[{ "_id": 48102938190, "nest": "airtel" },{_id":48102938190,巢":jio"}]},{_id":41092381029,"name": "2222",阵列3":[{ "_id": 10293182309, "nest": "master" },{_id":38190238,巢":幼崽"}]}],"name": "新公寓"}]}]

我想将 $filter 设置为 3 个嵌套级别...我只需要第三个数组中的 nokia 元素和第二个数组中的 6666 元素和旧公寓从第一个

我想要这个输出

<预><代码>[{数组1":[{阵列2":[{"name": "6666",阵列3":[{_id":12803918239,巢":诺基亚"}]}],"name": "旧公寓"}]}]

而且我还想只使用 $filter$map...不想在这里使用 $unwind

解决方案

基本上你必须对每个级别使用 $filter 来应用你的条件,并为每个级别使用 $map内部有嵌套数组的数组.那是因为您想将过滤后的数组传递给输出.所以会有 3 个过滤器和 2 个映射.在这种情况下,缩进可能真的很有帮助.试试:

db.collection.aggregate([{$项目:{数组 1:{$地图:{input: { $filter: { input: "$Array1", as: "a1", cond: { $eq: ["$$a1.name", "old apartment" ] } } },如:a1",在: {名称: "$$a1.name",数组2:{$地图:{input: { $filter: { input: "$$a1.Array2", as: "a2", cond: { $eq: [ "$$a2.name", "6666" ] } } },如:a2",在: {名称: "$$a2.name",Array3: { $filter: { input: "$$a2.Array3", as: "a3", cond: { $eq: [ "$$a3.nest", "nokia" ] } } }}}}}}}}}])

Mongo 游乐场

I have a below collection

[
  {
    "Array1": [
      {
        "Array2": [
          {
            "name": "6666",
            "Array3": [
              { "_id": 128938120, "nest": "samsung" },
              { "_id": 12803918239, "nest": "nokia" }
            ]
          },
          {
            "name": "5555",
            "Array3": [
              { "_id": 48102938109, "nest": "iphone" },
              { "_id": 501293890, "nest": "micromax" }
            ]
          }
        ],
        "name": "old apartment"
      },
      {
        "Array2": [
          {
            "_id": 410923810,
            "name": "3333",
            "Array3": [
              { "_id": 48102938190, "nest": "airtel" },
              { "_id": 48102938190, "nest": "jio" }
            ]
          },
          {
            "_id": 41092381029,
            "name": "2222",
            "Array3": [
              { "_id": 10293182309, "nest": "master" },
              { "_id": 38190238, "nest": "cub" }
            ]
          }
        ],
        "name": "new apartment"
      }
    ]
  }
]

I want to fo $filter to 3 nested level... I want only nokia element from 3rd array and 6666 element from second and old apartment from the first

I want this output

[
  {
    "Array1": [
      {
        "Array2": [
          {
            "name": "6666",
            "Array3": [
              {
                "_id": 12803918239,
                "nest": "nokia"
              }
            ]
          }
        ],
        "name": "old apartment"
      }
    ]
  }
]

And also I want to do using $filter and $map only... Don't want to use $unwind here

解决方案

Basically you have to use $filter for each level to apply your condition and $map for each array that has nested array inside. That's because you want to pass filtered array to the output. So there will be 3 filters and 2 maps. Indentations might be really helpful in this case. Try:

db.collection.aggregate([
    {
        $project: {
            Array1: {
                $map: {
                    input: { $filter: { input: "$Array1", as: "a1", cond: { $eq: ["$$a1.name", "old apartment" ] } } },
                    as: "a1",
                    in: {
                        name: "$$a1.name",
                        Array2: {
                            $map: {
                                input: { $filter: { input: "$$a1.Array2", as: "a2", cond: { $eq: [ "$$a2.name", "6666" ] } } },
                                as: "a2",
                                in: {
                                    name: "$$a2.name",
                                    Array3: { $filter: { input: "$$a2.Array3", as: "a3", cond: { $eq: [ "$$a3.nest", "nokia" ] } } }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
])

Mongo playground

这篇关于$filter 在 mongodb 中最多嵌套 3 个级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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