MongoDB Projection with Example

An In-Depth Look at Projection in MongoDB:

We learned more about Aggregation in MongoDB along with different commands and operators in our previous tutorial.

In this tutorial, we will take an in-depth look at the projection in MongoDB. We use projection when we want to retrieve only the selected fields of a whole document instead of all.

Explore the entire range of MongoDB Online Training Tutorials.

15. PROJECTION in mongodb

Projection in MongoDB

In MongoDB, table means “Collection”, row means “Document” and column means “Field”.

In simple words, the purpose of the projection is that it helps us to find selective data. We will have a huge amount of records when we deal with an enterprise level of applications. The Processing of these records will take a lot of time, thus we need only the selected data to process.

Following is a sample Syntax by which you can retrieve the limited amount of data using Projection in MongoDB.

Syntax:

db.DATA_COLLECTION_NAME.find({},{YOUR_FIELD_KEY:BOOLEAN})

Now it's time to understand the syntax to process projection.

DATA_COLLECTION_NAME is the name of the table from where you have to retrieve the records for processing.

Following is the sample Syntax by which you can retrieve the limited amount of data using Projection in MongoDB.

  • YOUR_FIELD_KEY is the name of the column or entity that you want to process from the table.
  • BOOLEAN is the check to show and hide the column value.

Let's have a look at the following example to understand the depth of projection by processing some data. By the following query, we will retrieve all the available record in the selected collection in a readable format with the help of pretty().

Query:

db.softwaretestinghelp.find().pretty()

Here “softwaretestinghelp” is the name of our collection. The processing of the above query will generate the following results.

Figure 1: Results in MongoDB Shell

pretty() mongodb

Figure 2: Results in Robo3T

pretty() robo3t

Now we are required to retrieve only the name of the student and all the other fields are unnecessary to be shown in the output. Hence, we will use projection here to slice some necessary information from the whole population of record.

In the following query, we are going to retrieve only the id of the student and hide the ObjectId from the record.

Query:

db.softwaretestinghelp.find({}, {"_id": 0, "student_id": 1})

This query will retrieve only the student id from our collection “softwaretestinghelp” and hide the ObjectId as you can observe in the following output.

Figure 3: Results in MongoDB Shell

retrieve only student id in mongodb

Figure 4: Results in Robo3T

retrieve only student id in robo 3t

In this example, we have used only one column name to show the record. If you wanted to show multiple column values with the help of projection in MongoDB, then you can use the below query.

Query:

db.softwaretestinghelp.find({}, {"_id": 0, "student_id": 1, "student_age": 1})

When you process the query having multiple column names with Boolean value 1, you will get the results as follows.

Figure 5: Results in MongoDB Shell

projection in MongoDB

Figure 6: Results in Robo3T

projection in robo 3t

Conclusion

Hope you are clear about the concept of projection in MongoDB.

Now we are able to process data and we found that Booleans are used to show and hide the column values.

Check out our upcoming tutorial to know more about Regular Expression in MongoDB!!

PREV Tutorial | NEXT Tutorial