I have a document that contains an array. Like so:
"_id" : ObjectId("55101f81e4b07caf8554b9b1"),
"myId" : "1222222",
"isDelayed" : false,
"status" : "BALLS",
"yellow" : false,
"white" : true,
"people" : [
{
"peopleId" : 222222,
"bc" : 0,
"status" : "live",
"fc" : 1,
"tc": 4,
"rc": "yellow"
},
{
"peopleId" : 33312,
"bc" : 0,
"status" : "live",
"fc" : 1,
"tc": 4,
"rc": "yellow"
},
...
I have a mongo query like below, in the collection mycoll, if myId=1.222 and in the people array if people.peopleId=1123 it returns the first match:
db.getCollection('mycoll').find(
{myId:'1.222',
people: { $elemMatch: { peopleId: 1123 }
}
},{"people.$": 1 }).pretty();
The result includes all fields in the people entry from the array:
"people" : [
{
"peopleId" : 1122,
"bc" : 0,
"status" : "live",
"fc" : 1,
"tc": 4,
"rc": "yellow"
},
How do I filter the reply that it only returns the desired field from the matching entry in the inner array, say "status"? I can generate a filter for the outer document but not a field in an array element.