I have a collection of contacts with the following structure:
{
"_id": "58f969646320ef4b5171dfe2",
"user_id": 1170,
"others": {
"campaigns": [
{
"campaign_id": 111,
"is_attendee": true,
"score": 25,
},
{
"campaign_id": 112,
"is_attendee": false,
"score": 22,
}
]
},
}
I want to return the contacts who belong to user 1170 but projecting only the first campaigns which have the ID in [111, 119] so I made the following query:
db.contacts.find({
"others.campaigns.user_id": 1170
}, {
"others.campaigns": {
$elemMatch: {
"campaign_id": {$in: [111, 119]}
}
},
{
"others.campaigns.$": 1
}
).limit(1).pretty()
but I got this error:
error: {
"ok" : 0,
"errmsg" : "Cannot use $elemMatch projection on a nested field.",
"code" : 2,
"codeName" : "BadValue"
}
So, is there any way to make the query work successfully?
Please: Discard the aggregation framework as a possible solution.