简体   繁体   中英

How to filter array (of objects) inside one document in mongo db based on some condition

I have the below docs collection structure. I'm able to filter the documnents with various approaches, but not able to filter the array inside the documents.

{
"_id": "",
"employee": {
    "EmployeeAttributeValues": {
        "EmployeeAttributeValue": [
            {.....
            },
            {.....
            },
            {.....
            },
            {.....
            }
        ]
    }
}

}

Kindly help me on how to filter the MemberAttributeValue array based on some condition.

you can use $where operator for custom filtering https://docs.mongodb.com/v4.2/reference/operator/query/where/

db.test.aggregate([
    { $match: {_id: <ID>}},
    { $unwind: '$<ARRAY>'},
    { $match: {'<ARRAY>.a': {$gt: 3}}},
    { $group: {_id: '$_id', list: {$push: '$<ARRAY>.a'}}}
])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM