MongoDB Projection

MongoDB projection is used to get the necessary data of the documents from the collection.

find() method

this method is used to get the documents from the collection.

Syntax:-


db.collectionName.find();

Example:- Suppose you want to get all records from the employees collection.


db.employees.find();

It will show all documents data and results will be in object format.

Output:-
{ “_id” : ObjectId(“5f26e736deec6e20ea057831”), “name” : “John”, “age” : 35, “department” : “department A”, “salary” : 200000 }
{ “_id” : ObjectId(“5f26e7c0deec6e20ea057832”), “name” : “Rom”, “age” : 30, “department” : “department A”, “salary” : 70000 }
{ “_id” : ObjectId(“5f26e9dedeec6e20ea057833”), “name” : “Tony”, “age” : 31, “department” : “department B”, “salary” : 40000 }
{ “_id” : ObjectId(“5f26e9dedeec6e20ea057834”), “name” : “Peter”, “age” : 32, “department” : “department B”, “salary” : 30000 }
{ “_id” : ObjectId(“5f26e9dedeec6e20ea057835”), “name” : “Andrew”, “age” : 33, “department” : “department C”, “salary” : 20000 }

find() method with where condition

first parameter in find() method is used for where condition.

Syntax:-


db.collectionName.find({where_condition});

Example:-
Suppose, you want to get record where department is department C


db.employees.find({department:"department C"});
Output
{ “_id” : ObjectId(“5f26e9dedeec6e20ea057835”), “name” : “Andrew”, “age” : 33, “department” : “department C”, “salary” : 20000 }

get the specific data through {key_name:1} from the collection

If you want to find specific data from the document then use two parameters where first parameter is used for where condition while second parameter is used to get specific data from the collection through {key_name:1}.
Syntax:-


db.collectionName.find({whereCondition},{key_name:1});

Example:-


db.employees.find({},{name:1});

Output:-

{ “_id” : ObjectId(“5f26e736deec6e20ea057831”), “name” : “John” }
{ “_id” : ObjectId(“5f26e7c0deec6e20ea057832”), “name” : “Rom” }
{ “_id” : ObjectId(“5f26e9dedeec6e20ea057833”), “name” : “Tony” }
{ “_id” : ObjectId(“5f26e9dedeec6e20ea057834”), “name” : “Peter” }
{ “_id” : ObjectId(“5f26e9dedeec6e20ea057835”), “name” : “Andrew” }

Note:- _id value will show automatically.

If you do not want _id value

_id will always display when you get the data if you do not want this then use _id:0


db.employees.find({},{_id:0,name:1});

Output:-

{ “name” : “John” }
{ “name” : “Rom” }
{ “name” : “Tony” }
{ “name” : “Peter” }
{ “name” : “Andrew” }

findOne() method

this method is used to get the only one record from the document.

Example:-


db.employees.findOne({},{name:1});

Output:-

{ “_id” : ObjectId(“5f26e736deec6e20ea057831”), “name” : “John” }

find().toArray() method

this method is used to get the result into Array format.


db.employees.find().toArray();

Output:-

[
{
“_id” : ObjectId(“5f26e736deec6e20ea057831”),
“name” : “John”,
“age” : 35,
“department” : “department A”,
“salary” : 200000
},
{
“_id” : ObjectId(“5f26e7c0deec6e20ea057832”),
“name” : “Rom”,
“age” : 30,
“department” : “department A”,
“salary” : 70000
},
{
“_id” : ObjectId(“5f26e9dedeec6e20ea057833”),
“name” : “Tony”,
“age” : 31,
“department” : “department B”,
“salary” : 40000
},
{
“_id” : ObjectId(“5f26e9dedeec6e20ea057834”),
“name” : “Peter”,
“age” : 32,
“department” : “department B”,
“salary” : 30000
},
{
“_id” : ObjectId(“5f26e9dedeec6e20ea057835”),
“name” : “Andrew”,
“age” : 33,
“department” : “department C”,
“salary” : 20000
}
]