Mongodb how to return a list of values ​​only from a search query

I have collections, each entry has fields: placement_id, program_id, category, ... I need to find all placements that have program_id = 3 or 5, and return only the place_id list.

when i tried this command:

db.placements.find({program_id:{$in: [3, 5]}}, {placement_id:1, _id:0}) 

I have entries:

 { "placement_id" : 196 } { "placement_id" : 197 } { "placement_id" : 198 } ... 

is there any way to return simply:

 [196, 197, 198...] 
+7
mongodb
source share
1 answer

The cursor from find() will produce JSON documents, no matter what. But you can extract the desired values. Something like this is possible:

 get_placement_id = function(doc) { return doc.placement_id; } db.placements.find({program_id:{$in: [3, 5]}}, {placement_id:1, _id:0}).map( get_placement_id ) 

==>

 [ 196, 197, 198, ... ] 
+13
source share

All Articles