MongoDB - Projection Queries

Various other features can be used to filter out our queries in a more precise manner. MongoDB allows its users to add more features to a query for extracting data from the Mongo database. In this chapter, you will learn about MongoDB Projection Queries which can be used for an additional purpose.

Projection Queries

Protection queries are a particular type of MongoDB queries where you can specify fields you want to get in the output. MongoDB allows you to perform a query for a collection by using the db.collection.find() method, where you have to mention the field that needs to be explicitly returned. This can be done by explicitly incorporating the field names in your query, and adding a 1 or 0 with them for specifying whether this needs to be returned or not. Such kinds of parameters are called projection parameter. When a projection parameter is associated with a value 1, it will show the value according to the query and hide when the projection parameter has a value 0.

Without Projection

Here is an example where the projection parameter is not used:


With Projection

Another example where the projection parameter is used:

db.writers.find( { "author": "Gaurav Mandes" }, { _id:0,  author:1, title:1 } )

In the example above, the _id field is excluded, which automatically gets added, and the title and author fields are displayed.

Scroll Back to Top