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

前端 未结 20 1244
执念已碎
执念已碎 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
    
    0 讨论(0)
  • 2020-11-22 08:28

    For better understanding I have written similar MySQL query.

    Selecting specific fields 
    

    MongoDB : db.collection_name.find({},{name:true,email:true,phone:true});

    MySQL : SELECT name,email,phone FROM table_name;

    Selecting specific fields with where clause
    

    MongoDB : db.collection_name.find({email:'you@email.com'},{name:true,email:true,phone:true});

    MySQL : SELECT name,email,phone FROM table_name WHERE email = 'you@email.com';

    0 讨论(0)
  • 2020-11-22 08:29

    While gowtham's answer is complete, it is worth noting that those commands may differ from on API to another (for those not using mongo's shell).
    Please refer to documentation link for detailed info.

    Nodejs, for instance, have a method called `projection that you would append to your find function in order to project.

    Following the same example set, commands like the following can be used with Node:

    db.student.find({}).project({roll:1})

    SELECT _id, roll FROM student

    Or
    db.student.find({}).project({roll:1, _id: 0})

    SELECT roll FROM student

    and so on.

    Again for nodejs users, do not forget (what you should already be familiar with if you used this API before) to use toArray in order to append your .then command.

    0 讨论(0)
  • 2020-11-22 08:29

    In mongodb 3.4 we can use below logic, i am not sure about previous versions

    select roll from student ==> db.student.find(!{}, {roll:1})

    the above logic helps to define some columns (if they are less)

    0 讨论(0)
  • 2020-11-22 08:31

    getting name of the student

    student-details = db.students.find({{ "roll": {$gt: 70} },{"name": 1, "_id": False})
    

    getting name & roll of the student

    student-details = db.students.find({{ "roll": {$gt: 70}},{"name": 1,"roll":1,"_id": False})
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题