I have the following documents in my collection:
{
"archives" : [
{ "colour" : "red", "default" : true },
{ "colour" : "green", "default" : false }
]
}
{
"archives" : [
{ "colour" : "yellow", "default" : true }
]
}
I want to project the colour value from the archive objects as follows:
{
"archives" : [ "red", "green" ]
}
{
"archives" : [ "yellow" ]
}
My proposal
My best attempt at this has been this query:
db.test.find({}, {
'archives': {
'$map': {
'input': '$archives',
'in': '$archives.colour'
}
}
})
But it's returning an array of arrays with redundant information, like so:
{ "archives" : [ [ "red", "green" ], [ "red", "green" ] ] }
{ "archives" : [ [ "yellow" ] ] }
So what would be the correct query to give the result I need, preferably on the database side, and as efficient as possible?