Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

javascript - $filter upto 3 nested level in mongodb

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...