<i id='En4S3'><tr id='En4S3'><dt id='En4S3'><q id='En4S3'><span id='En4S3'><b id='En4S3'><form id='En4S3'><ins id='En4S3'></ins><ul id='En4S3'></ul><sub id='En4S3'></sub></form><legend id='En4S3'></legend><bdo id='En4S3'><pre id='En4S3'><center id='En4S3'></center></pre></bdo></b><th id='En4S3'></th></span></q></dt></tr></i><div id='En4S3'><tfoot id='En4S3'></tfoot><dl id='En4S3'><fieldset id='En4S3'></fieldset></dl></div>

    <tfoot id='En4S3'></tfoot>
    1. <legend id='En4S3'><style id='En4S3'><dir id='En4S3'><q id='En4S3'></q></dir></style></legend>

        <small id='En4S3'></small><noframes id='En4S3'>

        • <bdo id='En4S3'></bdo><ul id='En4S3'></ul>

        $filter 在 mongodb 中最多 3 个嵌套级别

        时间:2023-10-02

              <tbody id='5AmSz'></tbody>
                <bdo id='5AmSz'></bdo><ul id='5AmSz'></ul>

                <small id='5AmSz'></small><noframes id='5AmSz'>

                <legend id='5AmSz'><style id='5AmSz'><dir id='5AmSz'><q id='5AmSz'></q></dir></style></legend>

                • <i id='5AmSz'><tr id='5AmSz'><dt id='5AmSz'><q id='5AmSz'><span id='5AmSz'><b id='5AmSz'><form id='5AmSz'><ins id='5AmSz'></ins><ul id='5AmSz'></ul><sub id='5AmSz'></sub></form><legend id='5AmSz'></legend><bdo id='5AmSz'><pre id='5AmSz'><center id='5AmSz'></center></pre></bdo></b><th id='5AmSz'></th></span></q></dt></tr></i><div id='5AmSz'><tfoot id='5AmSz'></tfoot><dl id='5AmSz'><fieldset id='5AmSz'></fieldset></dl></div>
                • <tfoot id='5AmSz'></tfoot>
                  本文介绍了$filter 在 mongodb 中最多 3 个嵌套级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下收藏

                  [
                    {
                      "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"
                        }
                      ]
                    }
                  ]
                  

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

                  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

                  我想要这个输出

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

                  我也想只使用 $filter$map ...不想在这里使用 $unwind

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

                  推荐答案

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

                  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 游乐场

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

                  上一篇:MongoDB:计算每个不同的值有多少? 下一篇:MongoDB在具有附加字段的对象数组上聚合$lookup

                  相关文章

                    <tfoot id='uT6ZQ'></tfoot>

                  1. <i id='uT6ZQ'><tr id='uT6ZQ'><dt id='uT6ZQ'><q id='uT6ZQ'><span id='uT6ZQ'><b id='uT6ZQ'><form id='uT6ZQ'><ins id='uT6ZQ'></ins><ul id='uT6ZQ'></ul><sub id='uT6ZQ'></sub></form><legend id='uT6ZQ'></legend><bdo id='uT6ZQ'><pre id='uT6ZQ'><center id='uT6ZQ'></center></pre></bdo></b><th id='uT6ZQ'></th></span></q></dt></tr></i><div id='uT6ZQ'><tfoot id='uT6ZQ'></tfoot><dl id='uT6ZQ'><fieldset id='uT6ZQ'></fieldset></dl></div>

                      • <bdo id='uT6ZQ'></bdo><ul id='uT6ZQ'></ul>

                      <small id='uT6ZQ'></small><noframes id='uT6ZQ'>

                      <legend id='uT6ZQ'><style id='uT6ZQ'><dir id='uT6ZQ'><q id='uT6ZQ'></q></dir></style></legend>