Projection

In MongoDB, projection means to get the necessary data of the document from the collections.

find():- this method is used to get the documents from the collections.
Syntax:-

db.collectionName.find();

Example:-

db.employees.find();

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

{ "_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 }

If you want to find specific data from the document then use projection.
Syntax:-

db.collectionName.find({whereCondition},{key: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 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:- 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():- 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
	}
]