Project Aggregation in MongoDB

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

When you do an aggregation in MongoDB, documents enter an aggregation pipeline. This pipeline can have multiple stages; at each stage of the pipeline, documents undergo some type of transformation. The transformed documents are then passed along to the next stage. In this article, we’ll focus on the $project stage of the aggregation pipeline– a handy tool that allows you to filter documents and display just the fields you need. We’ll show you how to perform project aggregation in MongoDB and provide some examples to better understand how it works.

Prerequisites

There are only a couple of prerequisites necessary for this tutorial on project aggregation in MongoDB:

  • MongoDB needs to be installed and configured on your system.
  • It’s best to have some basic knowledge of MongoDB to follow along with the examples in this article.

What is MongoDB $project?

The $project stage of the pipeline takes the specified fields in the document and passes them to the next stage of the aggregation pipeline. The fields can be existing fields from a target document or newly-created fields. As we mentioned earlier, this functionality makes the $project stage an effective filtering tool, allowing you to show only certain fields.

The $project stage has the following syntax:

1
{ $project: { <expression(s)> } }

We can see that $project accepts a MongoDB document that contains specifications. These specifications can include the addition of a new document field, inclusion of one or more document fields, exclusion of one or more document fields, a reset of current field values or the suppression of the ‘_id’ field.

Create Sample Data Set

Our first task will be to create a sample data set that we can use throughout this tutorial. Let’s create a collection named animals with several documents in it:

1
2
3
4
5
6
7
8
9
db.animals.insertMany([
    {_id:1,name: "zebra", quantity: 20},
    {_id:2,name: "lion", quantity: 10},
    {_id:3,name: "tigers", quantity: 16},
    {_id:4,name: "monkey", quantity: 24},
    {_id:5,name: "parrot", quantity: 75},
    {_id:6,name: "donkey" , quantity: 6},
    {_id:7,name: "jaguar", quantity: 8},
]);

Our new collection should look like this:

1
2
3
4
5
6
7
{ "_id" : 1, "name" : "zebra", "quantity" : 20 }
{ "_id" : 2, "name" : "lion", "quantity" : 10 }
{ "_id" : 3, "name" : "tigers", "quantity" : 16 }
{ "_id" : 4, "name" : "monkey", "quantity" : 24 }
{ "_id" : 5, "name" : "parrot", "quantity" : 75 }
{ "_id" : 6, "name" : "donkey", "quantity" : 6 }
{ "_id" : 7, "name" : "jaguar", "quantity" : 8 }

$project Specific Fields

In this section, we’ll walk through an example that uses the MongoDB $project aggregation against our ‘animal’ collection.

To perform our aggregation, we’ll use the following command:

1
db.animals.aggregate( [ { $project : { name : 1 , quantity : 1 } }] );

This command will pass along documents within the animals collection with the fields ‘name’ and ‘quantity’.

The result should look something like this:

1
2
3
4
5
6
7
{ "_id" : 1, "name" : "zebra", "quantity" : 20 }
{ "_id" : 2, "name" : "lion", "quantity" : 10 }
{ "_id" : 3, "name" : "tigers", "quantity" : 16 }
{ "_id" : 4, "name" : "monkey", "quantity" : 24 }
{ "_id" : 5, "name" : "parrot", "quantity" : 75 }
{ "_id" : 6, "name" : "donkey", "quantity" : 6 }
{ "_id" : 7, "name" : "jaguar", "quantity" : 8 }

Our next example will show just the ‘name’ field of documents. To do this, we’ll use the following command:

1
db.animals.aggregate( [ { $project : { name : 1, _id: 0} }] );

The output should look something like this;

1
2
3
4
5
6
7
{ "name" : "zebra" }
{ "name" : "lion" }
{ "name" : "tigers" }
{ "name" : "monkey" }
{ "name" : "parrot" }
{ "name" : "donkey" }
{ "name" : "jaguar" }

Notice that we suppressed the _id by setting the value to ‘0’ in our command.

Conclusion

The MongoDB aggregation pipeline can contain a number of different stages, and it’s important to understand how each stage works in order to perform effective aggregation operations. In this article, we took an in-depth look at the $project stage, which is used to filter specific fields in MongoDB documents and pass only these fields to the next stage in the pipeline. We discussed the syntax of $project and looked at some examples of its use. With this tutorial to guide you, you’ll be prepared to perform a project aggregation in MongoDB and get the results you need.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.