0
{
"_id" : "123",
"someKey" : "someValue",
"someArray" : [
    {
        "name" : "name1",
        "someNestedArray" : [
            {
                "name" : "value",
                "somenestedofnestedArray":[
                        {
                            "key":"one",
                            "checked":true
                        },
                        {   
                            "key":"two",
                            "checked":false
                        }
                ]
            },
            {
                "name" : "delete me"
            }
        ]
    }
  ]
}

i just want to get the result by filtering the somenestedofnestedArray in mongodb.

only i want to return the documents which has checked is true .

I have referred the below URL but still i unable to achieve. Find in double nested array

I tried like this

db.collection.aggregate([
{
    $addFields: {
        someArray: {
            $map: {
                input:"$someArray",
                as: "resultm",
                in: {
                   name: "$$resultm.name",
                   someNestedArray: {

                        $filter: {
                            input: "$$resultm.someNestedArray.somenestedofnestedArray",
                            as: "resultf",
                            cond: {
                                $eq: ["$$resultf.checked", true]
                            }
                        }
                    }
                }
            }
        }
    }
}
])
vinodh
  • 714
  • 1
  • 7
  • 20
  • So what about the answer there do you still not understand? Double, Triple, Quadruple, it does not matter. The same principles apply. No code attempt here. Show what you have tried and then we can see what you are doing wrong. Also (Ahem!) as the author of the referenced answer, I suggest you actually read it and begin to comprehend why nesting arrays is a really bad idea. – Neil Lunn Apr 16 '18 at 03:09
  • I just edited the question – vinodh Apr 16 '18 at 03:22
  • So that pretty much looks like a direct copy of my code demonstrating a double nesting. You do realize you essentially `$map` and `$filter` at **each** level of nesting, don't you? Because I do go to some detail of actually explaining that in the answer. I suggest you give it a real go, because I really don't see a need for a new answer for 3 levels of nesting or 4 or 5 or whatever, when the basic approach is exactly the same. – Neil Lunn Apr 16 '18 at 03:25
  • 1
    try to use `db.demo.aggregate([ { "$unwind": "$someArray" }, { "$unwind": "$someArray.someNestedArray" }, { "$unwind": "$someArray.someNestedArray.somenestedofnestedArray" }, { "$match": { "someArray.someNestedArray.somenestedofnestedArray.checked": true }}, ])` – gaurav singh Apr 16 '18 at 04:35

0 Answers0