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

前端 未结 20 1246
执念已碎
执念已碎 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);
    

    `

    0 讨论(0)
  • 2020-11-22 08:46
    db.student.find({}, {"roll":1, "_id":0})
    

    This is equivalent to -

    Select roll from student



    db.student.find({}, {"roll":1, "name":1, "_id":0})

    This is equivalent to -

    Select roll, name from student

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

    If u want to retrieve the field "roll" only for all 10 records in the collections. Then try this.

    In MongoDb :

    db.students.find( { } , { " roll " : { " $roll " })

    In Sql :

    select roll from students

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

    Use the Query like this in the shell:

    1. Use database_name

    e.g: use database_name
    

    2. Which returns only assets particular field information when matched , _id:0 specifies not to display ID in the result

    db.collection_name.find( { "Search_Field": "value" }, 
                      { "Field_to_display": 1,_id:0 }  )
    
    0 讨论(0)
  • 2020-11-22 08:48

    I just want to add to the answers that if you want to display a field that is nested in another object, you can use the following syntax

    db.collection.find( {}, {{'object.key': true}})

    Here key is present inside the object named object

    { "_id" : ObjectId("5d2ef0702385"), "object" : { "key" : "value" } }
    
    
    0 讨论(0)
  • 2020-11-22 08:49

    get all data from table

    db.student.find({})
    

    SELECT * FROM student


    get all data from table without _id

    db.student.find({}, {_id:0})
    

    SELECT name, roll FROM student


    get all data from one field with _id

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

    SELECT id, roll FROM student


    get all data from one field without _id

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

    SELECT roll FROM student


    find specified data using where clause

    db.student.find({roll: 80})
    

    SELECT * FROM students WHERE roll = '80'


    find a data using where clause and greater than condition

    db.student.find({ "roll": { $gt: 70 }}) // $gt is greater than 
    

    SELECT * FROM student WHERE roll > '70'


    find a data using where clause and greater than or equal to condition

    db.student.find({ "roll": { $gte: 70 }}) // $gte is greater than or equal
    

    SELECT * FROM student WHERE roll >= '70'


    find a data using where clause and less than or equal to condition

    db.student.find({ "roll": { $lte: 70 }}) // $lte is less than or equal
    

    SELECT * FROM student WHERE roll <= '70'


    find a data using where clause and less than to condition

    db.student.find({ "roll": { $lt: 70 }})  // $lt is less than
    

    SELECT * FROM student WHERE roll < '70'


    0 讨论(0)
提交回复
热议问题