Visit complete MongoDB roadmap

← Back to Topics List

MongoDB Aggregation

MongoDB Aggregation framework provides a way to process and transform data that is stored in our MongoDB collections. It allows you to perform calculations and return the calculated results using various data aggregation tools such as aggregation pipelines, map-reduce functions, or single-purpose aggregation methods.

Here is a brief summary of MongoDB Aggregation:

Aggregation Pipeline

The aggregation pipeline is a framework in MongoDB that enables the developers to execute a series of data transformations on the documents in a collection. The pipeline consists of multiple stages, and each stage applies a specific operation on the input documents. Among these operations, you can find features like filtering, sorting, projecting, and grouping.

Example of a simple aggregation pipeline:

  { $match: { status: 'A' } },
  { $group: { _id: '$cust_id', total: { $sum: '$amount' } } },
  { $sort: { total: -1 } },


Map-Reduce is another method to aggregate data in MongoDB. It involves defining a map function to extract data from the input documents, which emits key-value pairs. A reduce function combines the emitted data by keys and optionally a finalize function to further process the results.

Example of a simple map-reduce function:

  function () {
    emit(this.cust_id, this.amount);
  function (key, values) {
    return Array.sum(values);
    query: { status: 'A' },
    out: 'order_totals',

Single-Purpose Aggregation

MongoDB also supports single-purpose aggregation methods, such as db.collection.count(), db.collection.distinct(), and etc. These methods offer a faster and more convenient way to perform simple aggregations directly.

Example of db.collection.count():

db.collection.count({ status: 'A' });

In conclusion, MongoDB Aggregation is a powerful feature that helps you extract, manipulate and aggregate data from your collections. By using aggregation pipelines, map-reduce functions or single-purpose aggregation methods, you can perform various data analysis tasks efficiently on your MongoDB dataset.

Community is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© · FAQs · Terms · Privacy


The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.