mongodb how to return list of value only from find query

后端 未结 1 1239
挽巷
挽巷 2021-01-11 17:07

i have a collection placements, each record has fields: placement_id, program_id, category, ... i need to find all placements what has program_id = 3 or 5 and only return a

相关标签:
1条回答
  • 2021-01-11 17:26

    The cursor from find() is going to yield JSON documents, no matter what. But you can extract the values you want. Something like this perhaps :

    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, ... ]
    
    0 讨论(0)
提交回复
热议问题