0

In my mongodb document, I have array inside array & I want to put filter on it in projection.

Sample Document:

{
    "_id" : ObjectId("5a3ba402cc96937e91e2861d"),
    "id" : 1,
    "name" : "a",
    "var" : [ 
        {
            "id" : 11,
            "name" : "aa",
            "data" : [ 
                {
                    "type" : 1,
                    "value" : "A"
                }, 
                {
                    "type" : 2,
                    "value" : "B"
                }
            ]
        }, 
        {
            "id" : 12,
            "name" : "ab",
            "data" : [ 
                {
                    "type" : 26,
                    "value" : "Z"
                }, 
                {
                    "type" : 10,
                    "value" : "J"
                }
            ]
        }
    ]
}

When I run following query,

db.harshit.aggregate([{
    $match: { "id" : 1}
}, {
    $project: {
        "_id" : 1,
        "id" : 1,
        "name" : 1,
        "var" : {
            $filter: {
                input: "$var",
                as: "var1",
                cond: {
                    "$eq": ["$$var1.id", 12]
                }
            }
        }
    }
}
])

then it gives expected result

{
    "_id" : ObjectId("5a3ba402cc96937e91e2861d"),
    "id" : 1,
    "name" : "a"
    "var" : [ 
        {
            "id" : 12,
            "name" : "ab",
            "data" : [ 
                {
                    "type" : 26,
                    "value" : "Z"
                }, 
                {
                    "type" : 10,
                    "value" : "J"
                }
            ]
        }
    ]
}

but if I use following query

db.harshit.aggregate([{
    $match: { "id" : 1}
}, {
    $project: {
        "_id" : 1,
        "id" : 1,
        "name" : 1,
        "var" : {
            $filter: {
                input: "$var",
                as: "var1",
                cond: {
                    "$eq": ["$$var1.data.type", 26]
                }
            }
        }
    }
}
])

then it gives empty array for "var"

{
    "_id" : ObjectId("5a3ba402cc96937e91e2861d"),
    "id" : 1,
    "name" : "a"
    "var" : []
}

I want to put filter on nested array "var.data" and want to project the objects that match the condition.

How do I filter those nested arrays?

Harshit
  • 1,334
  • 1
  • 9
  • 23
  • 1
    Use $in instead of $eq and switch the operands. Something like `"$in": [26, "$$var1.data.type"]`. It should filter outer array when there is a matching condition in inner array.. You can use [solution](https://stackoverflow.com/q/29071748/2683814) for filtering the inner array. – s7vr Dec 21 '17 at 13:23
  • `"$in": [26, "$$var1.data.type"]` gives error: invalid operator '$in' – Harshit Dec 21 '17 at 13:42
  • 1
    It looks like you are not on 3.4. For lower versions try `$setIsSubset: [[26], "$$var1.data.type"]` instead. – s7vr Dec 21 '17 at 13:47
  • it works perfect using the [solution](https://stackoverflow.com/questions/29071748/find-in-double-nested-array-mongodb) that you provided, thanks. – Harshit Dec 21 '17 at 14:01
  • `$setIsSubset: [[26], "$$var1.data.type"]` also works perfectly and it's easier than above solution. Now I am confused which one to use? – Harshit Dec 22 '17 at 05:14
  • These are two different solutions. One provided in comment filters the outer array based on condition ok inner arrays and the other solution linked filters both inner and outer arrays. It looks like you are looking for first one. – s7vr Dec 22 '17 at 07:58

0 Answers0