I want to filter the array object based on value provided. I am using following query in Filter text box and along with projection. Seems like there filter is not getting applied.
I also tried following but unfortunately array is not getting filtered
{ MissingPersonIds : {$elemMatch: { PhotoId : '2 - Copy (3).jpg'}} }
Filter:
{ "MissingPersonIds.PhotoId" : "2 - Copy (3).jpg" }
Projection:
{ MissingPersonIds : { $slice: [1,10] }}
Schema is:
[{
"_id": {
"$oid": "61ada7da9a30fd8471869bbc"
},
"ImportKeyId": 5843,
"MissingPersonIds": [
{
"PhotoId": "2 - Copy.jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "2 - Copy (2).jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "2 - Copy (3).jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "2 - Copy - Copy.jpg",
"Description": "Account ID not found"
},
{
"PhotoId": "2 - Copy - Copy (2).jpg",
"Description": "Account ID not found"
}
]
}]
The output I am expecting is :
[{
"_id": {
"$oid": "61ada7da9a30fd8471869bbc"
},
"ImportKeyId": 5843,
"MissingPersonIds": [
{
"PhotoId": "2 - Copy (3).jpg",
"Description": "Account ID not found"
}
]
}]
CodePudding user response:
You need $filter
in the projection to filter element(s) in an array.
db.collection.find({},
{
"MissingPersonIds": {
$filter: {
input: "$MissingPersonIds",
cond: {
"$eq": [
"$$this.PhotoId",
"2 - Copy (3).jpg"
]
}
}
}
})