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

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


提交回复
热议问题