I have a collection in my DB where each item looks like this :
{
"_id" : ObjectId("XXXXXX"),
"name" : "foo",
"products" : [{
"name" : "bar",
"code" : 123
},{
"name" : "foo",
"code" : 321
}]
}
And I want to get from a query a single array containing : [123, 321] from a specific collection.
This is what I have attempted :
db.getCollection('catalog').aggregate({
"$project": {
"products": {
"$map": {
"input": "$products",
"as" : "item",
"in" : "$$item.code"
}
}
}
})
But it fails and it stats "Last stage is undefined".
What am I doing wrong?