How to select a single field for all documents in a MongoDB collection?

前端 未结 20 1574
执念已碎
执念已碎 2020-11-22 07:58

In my MongoDB, I have a student collection with 10 records having fields name and roll. One record of this collection is:

{
    \"         


        
20条回答
  •  无人共我
    2020-11-22 08:36

    Using Studio 3T for MongoDB, if I use .find({}, { _id: 0, roll: true }) it still return an array of objects with an empty _id property.

    Using JavaScript map helped me to only retrieve the desired roll property as an array of string:

    var rolls = db.student
      .find({ roll: { $gt: 70 } }) // query where role > 70
      .map(x => x.roll);           // return an array of role
    

提交回复
热议问题