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

前端 未结 20 1545
执念已碎
执念已碎 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:25

    Here you go , 3 ways of doing , Shortest to boring :

    db.student.find({}, 'roll _id'); // <--- Just multiple fields name space separated
    // OR
    db.student.find({}).select('roll _id'); // <--- Just multiple fields name space separated
    // OR
    db.student.find({}, {'roll' : 1 , '_id' : 1 ); // <---- Old lengthy boring way
    

    To remove specific field use - operator :

    db.student.find({}).select('roll -_id') // <--- Will remove id from result
    

提交回复
热议问题