Get specific field in array mongodb and return as array -
i have following document:
{ array: [ { type: 'error', data: 1 }, { type: 'error', data: 2 } ] } is there anyway data field in each array element , return array? following:
[1, 2] // <--- array containing data fields the mongodb projection documentation doesn't seem cover this?
you can use aggregation , $map operator.
db.collection.aggregate([ { "$project": { "_id": 0, "data": { "$map": { "input": "$array", "as": "ar", "in": "$$ar.data" } } } } ]) which yields:
{ "data" : [ 1, 2 ] }
Comments
Post a Comment