Why Should You Aggregate Your Queries in MongoDB?

Nidhin MNidhin M
3 min read

If you've been using .find() and .update() for most of your MongoDB queries, you're only scratching the surface. MongoDB's Aggregation Framework is like a mini data pipeline that helps you transform, analyze, and reshape your data—all inside the database.

In this blog, I’ll explain why aggregation matters, where it's better than traditional queries, and how I’ve used it in real-world scenarios like calculating customer visits and summarizing booking data.


Why Aggregation?

  • 🔹 Simple queries give you raw data; aggregation gives you insights.

  • 🔹 You can group, sort, filter, reshape, and compute values in one query.

  • 🔹 It's often faster than fetching raw data and processing in code.

  • 🔹 Helps with building dashboards, reports, analytics, or real-time stats.

Example Use Case:
Imagine you're building a hotel booking app and want to find:

  • Total bookings per customer

  • First and last booking date

  • Number of visits by email per hotel

→ That’s where aggregation shines.


What is an Aggregation Pipeline?

Think of aggregation as a series of data-processing steps, like a data conveyor belt. Each stage in the pipeline takes in documents, transforms them, and passes them to the next.

// Example: Group bookings by email

db.bookings.aggregate([
  { $match: { hotel_id: "H001" } },
  { $group: { _id: "$email", totalBookings: { $sum: 1 } } },
  { $sort: { totalBookings: -1 } }
]);

Each stage is a transformation. Combined together, they build powerful pipelines.


Most Useful Aggregation Stages

StageWhat it doesExample usage
$matchFilter documentsFind bookings for hotel X
$groupGroup by fields & aggregateCount bookings per customer
$projectReshape fieldsExclude or rename fields
$sortSort outputSort by total visits
$limitLimit outputGet top 5 active customers
$lookupJoin another collectionCombine booking with customer info

Real-World Use Case: My Aggregation for Booking System

In my hotel channel manager app, I had to link each booking to a customer using just their email and hotel ID. To avoid duplicating customers, I aggregated bookings grouped by email + hotel, calculated visits, and saved them to a hotel_customers collection.

Here’s the actual pipeline I used:

[
  {
    $group: {
      _id: { email: "$email", hotel_id: "$hotel_id" },
      bookings: { $push: "$_id" },
      name: { $first: "$name" },
      mobile: { $first: "$mobile" },
      visitCount: { $sum: 1 }
    }
  }
]

This helped us de-duplicate customers and associate historical data more efficiently.


Performance Tips

  • ✅ Use $match early in the pipeline to filter as soon as possible.

  • ✅ Use $project to trim unneeded fields before heavy operations.

  • ✅ Index fields used in $match, $lookup, and $group.

  • ✅ For large data sets, use { allowDiskUse: true }.

  • ✅ Test pipelines using MongoDB Compass or Playground.


Common Mistakes to Avoid

  • ❌ Using $addToSet when $push was needed (or vice versa).

  • ❌ Forgetting to normalize fields (e.g., lowercase emails).

  • ❌ Grouping by entire objects instead of specific fields.

  • ❌ Writing everything in one stage—break it into readable chunks.


Conclusion

Aggregation isn't just for analytics—it's for writing smarter, more efficient queries. Whether you're summarizing data, building dashboards, or cleaning up records, aggregation pipelines help you do more with less code and fewer database calls.

Try replacing a few .find() + .forEach() loops in your code with a single aggregation pipeline—you’ll be surprised how powerful it is.

Also i found this youtube video helpful you can also check that out:
https://www.youtube.com/watch?v=MWmMvudBgFU

If you found this post helpful or have questions about your use case, feel free to reach out or connect with me on LinkedIn!

0
Subscribe to my newsletter

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

Written by

Nidhin M
Nidhin M

Hey there! I'm a passionate software developer who thrives in the TypeScript world and its companions. I enjoy creating tutorials, sharing tech tips, and finding ways to boost performance. Let's explore coding and tech together! 🚀