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

ID : 10155

viewed : 13

Tags : mongodbprojectionmongo-collectionmongodb

Top 5 Answer for projection - How to select a single field for all documents in a MongoDB collection?

vote vote

90

From the MongoDB docs:

A projection can explicitly include several fields. In the following operation, find() method returns all documents that match the query. In the result set, only the item and qty fields and, by default, the _id field return in the matching documents.

db.inventory.find( { type: 'food' }, { item: 1, qty: 1 } )

In this example from the folks at Mongo, the returned documents will contain only the fields of item, qty, and _id.


Thus, you should be able to issue a statement such as:

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

The above statement will select all documents in the students collection, and the returned document will return only the roll field (and exclude the _id).

If we don't mention _id:0 the fields returned will be roll and _id. The '_id' field is always displayed by default. So we need to explicitly mention _id:0 along with roll.

vote vote

90

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'

vote vote

80

I think mattingly890 has the correct answer , here is another example along with the pattern/commmand

db.collection.find( {}, {your_key:1, _id:0})

enter image description here

vote vote

62

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 
vote vote

58

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.

Top 3 video Explaining projection - How to select a single field for all documents in a MongoDB collection?

Related QUESTION?