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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -