get only one field from mongodb collection code example

Example 1: mongodb select specific fields

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

Example 2: mongodb select fields

db.inventory.find( { status: "A" }, { item: 1, status: 1 } )

Example 3: find only one field mongodb

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

Example 4: get custom fields in result of mongodb query

db.inventory.find( { age: "10" }, { _id: 0 ,  age: 1, user: { name: 1 } } )

Example 5: get only some fields of document in mongodb

db.student.find({}, {roll:1, _id:0})
 same as SELECT roll FROM student

Tags:

Go Example