Aggregation using MongoDB compass

Aishwarya SAishwarya S
3 min read

Aggregation:

Aggregation in MongoDB is used to process

  • Multiple documents

  • Across collections

  • Grouping values to provide a single output

Aggregation process the data by splitting it into multiple stages. Multiple stages combine to form a single pipeline.

In the MongoDB aggregation pipeline, each stage's output is given as input to the next stage.

Demo:

Created a simple Movie ticket booking system for demonstration. ‘users’ is one of the collections in the movie ticket booking system.

In every collection, the aggregation tab will be there in which you can perform aggregation operations for that particular collection.

Here, I have taken users collection for a demo.

Saving pipeline:

You can save your pipeline by clicking the ‘Save’ icon

Viewing saved pipelines

You can create one or more pipelines in a single collection itself. To view your saved pipelines, click on the ‘folder’ icon which shows the list of pipelines in your collection.

Creating stage in the pipeline:

Each pipeline consists of multiple stages whereas each stage’s output is fed as the input to the next stage in the pipeline.

In each stage, you can use any of the aggregation queries based on your use case.

You can write the query for the selected stage and the output for that query will be displayed on the right side for each document.

Adding a new stage in the pipeline:

You can add the new stage in the pipeline by clicking the ‘+’ icon.

Delete stage:

Any stage in the pipeline can be deleted by simply clicking the ‘delete’ icon

Turn off Stage:

In case you don’t want to delete the stage, you can turn off that stage from the pipeline.

Creating view:

The view is used to create a new collection that stores the output of the pipeline after all stages but with read-only mode.

Viewing the view:

The view displays the output of the pipeline after all stages in read-only mode.

Update View:

If you want to add new stage in the pipeline whose output should be reflected in the view, you have to edit the View.

Once ‘Edit View’ is clicked, it will take us to the aggregation tab where you can alter any stages or create a new stage in the pipeline.

Here, I have added a new stage in the pipeline ie. Added a new entry in the document. After altering any stages, Click update View.

After updating, the view will be updated with the new changes.

------------------ THANKS FOR READING ---------------------

1
Subscribe to my newsletter

Read articles from Aishwarya S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aishwarya S
Aishwarya S