In MongoDB, projection means selecting only the necessary data rather than selecting whole of the data of a document.
The basic syntax of find()
method with projection is as follows
> db.COLLECTION_NAME.find({},{KEY:1});
If you want to to show all document without the age field then the command is as follows
> db.people.find({},{age:0});
If you want to show only the age field then the command is as follows
> db.people.find({},{age:1});
Note: _id
field is always displayed while executing find()
method, if you don't want this field, then you need to set it as 0
.
> db.people.find({},{name:1,_id:0});
Note: 1
is used to show the field while 0
is used to hide the fields.