I would like to return specific fields from an array in Mongo and am having trouble.
Let us say we have a document like such:
{
"student":"Bob",
"report_cards": [
{
"Year":2016,
"English":"B",
"Math":"A"
},
{
"Year":2015,
"English":"B",
"Math":"A"
}
]
}
I would like to return the following:
{"Student": "Bob", {"English":"B"}}
Basically, I only need the first element in the report cards array, and only return the English field.
I know it is something around:
db.collection.find({},{"Student":1, "report_cards":{$slice:1}});
But this, of course, results in the full array (year, english, math) returning. I have tried the following:
db.collection.find({},{"Student":1, "report_cards.$.english":{$slice:1}})
db.collection.find({},{"Student":1, "report_cards":{$slice:1, "$.english"}});
but those aren't correct.
How do I simply return one field from the obj within the array results?
Thank you!