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

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

    Just for educational purposes you could also do it with any of the following ways:

    1.

        var query = {"roll": {$gt: 70};
        var cursor = db.student.find(query);
        cursor.project({"roll":1, "_id":0});
    

    2.

        var query = {"roll": {$gt: 70};
        var projection = {"roll":1, "_id":0};
        var cursor = db.student.find(query,projection);
    

    `

提交回复
热议问题