MongoDB find() method with Projection

You may not want to fetch a whole bunch of data from the document which is stored in a collection in your MongoDB database. You can restrict the number of records or data using different conditions with the help of WHERE, AND, OR and IN operators, but what if you want to restrict the data you want in your particular row or record or document. So here comes the projection that helps you to selectively choose data from a record or row or document.

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 in MongoDB is given below:



MongoDB 4.4.0, MongoDB Setup

Sample Data

As a first step I want to insert some sample data into user collection under roytuts database. Execute below insert() statements in the MongoDB shell to insert these data.


Projection Examples

Here I am going to show you how to work with projection in find() method while you are fetching documents from collection in MongoDB. If you want to show all documents without the phone field then the command is as follows:

db.roytuts.user.find({},{phone : 0});

The above query will give you the following output:

find() method with projection in mongodb

You see in the above output there is no phone field in the returned results.

If you want to to show all document without the secondName field then the command is as follows:

db.roytuts.user.find({},{secondName : 0});

The above query will produce the following result:

> db.roytuts.user.find({},{secondName : 0});
{ "_id" : ObjectId("5f855a90a8e8239c6a8022f8"), "firstName" : "Soumitra", "address" : { "permanent" : "Earth", "current" : "Any" }, "phone" : "1234567890" }
{ "_id" : ObjectId("5f855a91a8e8239c6a8022f9"), "firstName" : "John", "address" : { "permanent" : "Earth", "current" : "Jupitor" }, "phone" : "1234562890" }
{ "_id" : ObjectId("5f855a91a8e8239c6a8022fa"), "firstName" : "Ikra", "address" : { "permanent" : "Earth", "current" : "Mars" }, "phone" : "1234567924" }
{ "_id" : ObjectId("5f855a92a8e8239c6a8022fb"), "firstName" : "Jolly", "address" : { "permanent" : "Earth", "current" : "Saturn" }, "phone" : "1284167924" }

If you want to show documents with phone field only then the command is as follows:

db.roytuts.user.find({},{phone : 1});

This will produce the following output:

> db.roytuts.user.find({},{phone : 1});
{ "_id" : ObjectId("5f855a90a8e8239c6a8022f8"), "phone" : "1234567890" }
{ "_id" : ObjectId("5f855a91a8e8239c6a8022f9"), "phone" : "1234562890" }
{ "_id" : ObjectId("5f855a91a8e8239c6a8022fa"), "phone" : "1234567924" }
{ "_id" : ObjectId("5f855a92a8e8239c6a8022fb"), "phone" : "1284167924" }

But one thing to notice here that in the above output the _id field is fetched even if you do not want to fetch it (in the above example). To exclude the _id field use the following command.

db.roytuts.user.find({},{phone : 1,_id:0});

Now you will see the above query produces the following output:

> db.roytuts.user.find({},{phone : 1,_id:0});
{ "phone" : "1234567890" }
{ "phone" : "1234562890" }
{ "phone" : "1234567924" }
{ "phone" : "1284167924" }

Note that 1 is used to show the field while 0 is used to hide the fields. Even if you put greater than 1 (for example, 2 or 3) in place of 1 in the above query, still you will see the same output.

That’s all and hope you got an idea how to use projection with find() method in MongoDB.

Leave a Comment