How to Select Data with MongoDB Projection

While getting the data from a MongoDB database, you may want to define the fields according to your choice. This can be achievable using the functionality which is known as MongoDB projection. You may want to only get the specific data fields from the database collection and ignore the rest. Thereby, the MongoDB projection functionality is going to make use of the query to only extract the specified fields from the database collection that meet the parameters. This can be very effective when working with huge collections. Within this guide, we will demonstrate the different types of MongoDB projections.

Create a Collection

The following code snippet is a MongoDB shell command that creates a new collection named “Data” in the current “test” database. The “test” is the MongoDB shell prompt which indicates that you are working in the “test” database. The MongoDB command db.createCollection(“Data”) is used to create a new collection named “Data” in the “test” database. The “db” object in the MongoDB shell represents the current database context, and createCollection() is a method that is utilized to create a new collection. After successfully creating the “Data” collection, MongoDB returns an acknowledgment object { ok: 1 }: showing that the query processing is successful.

test> db.createCollection("Data")


Insert the Records

The provided code adds multiple records into a MongoDB collection named “Data”. Each record represents a person with fields for “name”, “age”, and “email”. The insertMany() function is utilized to insert more than one record into the “Data” collection. The insertMany() function yields an object with two properties. The acknowledged property is set to true which indicates that the insertion is successful. The “insertedIds” property contains the unique _id values produced for each inserted record. In this case, five records are inserted and each record has its specific _id.

  { name: "John", age: 44, email: "" },
  { name: "Ana", age: 28, email: "" },
  { name: "Nina", age: 67, email: "" },
  { name: "Cillian", age: 28, email: "" },
  { name: "Barbie", age: 22, email: "" }


Inclusion Projection

Instead of receiving all the records from the specific collection of a database, you may utilize the inclusion projection to only get the specific fields of the collection. When you just need a specific information and you want to minimize the data output that is carried over the network, this is quite accommodating. The second parameter to the find() method is utilized to set the fields that you’d like to display in the shell result via utilizing the Inclusion Projection.


The provided code is a MongoDB shell query that fetches the data from a “Data” collection. The query is used to find all records in the collection and returns only the “name” and “email” fields for each document record. The “test>” indicates the current MongoDB shell session, and the database that you are connected to is named “test”.

The “db.Data.find({}, {name: 1, email: 1})” is the MongoDB inclusion projection query. It uses the “find()” method on the “Data” collection. The first argument “{}” is the query filter. It’s an empty object “{}” which means that we are not utilizing any filtering condition, and we want to retrieve all records from the collection. The second argument “{name: 1, email: 1}” is the projection record. It specifies that we want to include only the “name” and “email” fields in the query output. The value of “1” means to include the field in the output.

test> db.Data.find({}, {name: 1, email: 1})

The output of the previous command is an array of objects. Each object in the array represents a record from the “Data” collection that matches the criteria. All five records contain the “name” and “email” fields along with an auto-generated “_id” field.

Exclusion Projection

Just like you use the inclusion projection to get the particular fields of the record, you may restrict the fetching of some records of the collection via the exclusion projection. The purpose of inclusion projection and exclusion projection is the same, i.e. to minimize the quantity of data that is carried over the network. The second parameter to the find() function is likely to specify the records that you don’t want to appear in the result.


The provided code illustrates the exclusion projection that is performed on a “Data” collection. The db.Data.find({}) part of the query is using the find() method to retrieve the records from the “Data” collection. The empty object “{}” fetches all the records from the “Data” collection. The “{ name: 0, email: 0 }” projection is cast off as the second parameter of the find() method.

In this case, the projection specifies that both the “name” and “email” fields should be excluded from the output by assigning the value of “0” to them.

test> db.Data.find({}, {name: 0, email: 0})

So, the result of the previously-specified query is an array of records that comprise only the “_id” and “age” fields, while the “name” and “email” fields are omitted from the result due to the use of projection. The “_id” field is always involved by default unless explicitly omitted, so it seems in the result:

Inclusion and Exclusion Combined

We cannot conduct the inclusion and exclusion projections simultaneously in one projection record in MongoDB. One strategy must be chosen above the other. To get the desired outcome, you can combine the inclusion and exclusion projections in different projection code records or queries.


The given code is a MongoDB query that tries to get the data from the “Data” collection using a projection. In this specific query, the code tries to add the “name” and “age” fields while excluding the “_id” and “email” fields. The reason is to get the records with only the “name” and “age” fields displayed in the output while ignoring the “_id” and “email” fields.

Though, the query comes across an exception and the MongoDB server yields the “MongoServerError” error with the “Cannot do exclusion on field email in inclusion projection” message.

test> db.Data.find({}, {name: 1, age: 1, _id: 0, email: 0})

The error message indicates that it is not probable to use both inclusion (specifying fields to include with a value of 1) and exclusion (specifying fields to exclude with a value of 0) projections together in the same query:


The MongoDB projection functionality permits you to modify the output of queries, letting you decide which fields to display or hide, thus refining the query performance and lessening the amount of the data transported over the net. Along with that, we discussed how both the different forms of projections can’t be used combined.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.