Aggregation in MongoDB Compass
Aggregation in MongoDB Compass
What is Aggregation in MongoDB?
Aggregation in MongoDB is used to process and transform multiple documents and even data across collections.
Here’s what aggregation lets you do:
-
Group values to produce a single summarized output
-
Run calculations (like sums, averages, counts)
-
Filter, sort, and reshape data — all in one go
MongoDB uses an aggregation pipeline.
-
Think of it as a conveyor belt: data flows through multiple stages.
-
Each stage processes the data and passes the output to the next stage.
Demo Setup – Movie Ticket Booking System
For this guide, I created a simple movie ticket booking system.
We’ll focus on the users
collection for our aggregation demo.
Inside every collection in Compass, there’s an Aggregation tab — this is where all the action happens.
Saving Pipelines
Once you’ve built a pipeline you like, you can save it for future use.
Saved pipelines are useful when you want to reuse complex queries.
Viewing Saved Pipelines
You can create multiple pipelines in a single collection.
To view them:
Creating a Stage in the Pipeline
Each pipeline is made up of multiple stages.
-
Each stage does one job (filtering, grouping, sorting, etc.).
-
The output of one stage becomes the input for the next.
You can write a query for each stage, and the output appears on the right side instantly.
Adding a New Stage
Need another stage? Just click the ‘+ Add Stage’ icon.
Compass makes it easy to stack as many stages as you need.
Deleting a Stage
Any stage can be deleted by clicking the delete (trash) icon.
Turning Off a Stage
If you don’t want to delete a stage but don’t need it right now, you can toggle it off. This way, it won’t affect the pipeline, but you can turn it back on later.
Creating a View
You can create a View from your pipeline.
A View is like a “virtual collection” that stores the final output of your pipeline in read-only mode.
Viewing the View
Once you create a View, you can open it like any other collection — it shows the final, read-only output of your pipeline.
Updating a View
If you want to tweak the pipeline behind a View:
-
Click Edit View — Compass takes you back to the Aggregation tab.
-
Add or remove stages, then click Update View.
Conclusion
Aggregation in MongoDB Compass takes the complexity out of building queries.
With just a few clicks, you can:
-
Build multi-stage pipelines
-
Save and reuse them
-
Create read-only Views for reporting
MongoDB Compass makes it visual, intuitive, and beginner-friendly — so you can spend less time fighting syntax and more time analyzing data.
--------------------------------------------------------THANK YOU---------------------------------------------------
Helpful 👍🏼
ReplyDelete🙌🙌🙌
ReplyDeleteGreat work
ReplyDelete💯💯
ReplyDeleteinformative
ReplyDeletenicely written👍
ReplyDelete