Aggregation in MongoDB

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:03:00

What is the Aggregation Framework?

The Aggregation Framework is a tool that transforms and combines documents in a collection, returning results as arrays. It creates a pipeline for processing documents, including transformations, grouping, sorting, limiting, and skipping.

What is Aggregation in MongoDB?

MongoDB aggregation operations collect and compute values from data records/documents, collect them, and execute operations such as total, average, minimum, and maximum. It works similarly to SQL's aggregate function, allowing for quick data translation, filtering, and analysis across numerous documents.

Ways to Aggregate Data in MongoDB

MongoDB provides three ways to perform aggregation:

  1. Aggregation Pipeline
  2. Map-Reduce Function
  3. Single-Purpose Aggregation Methods

Aggregation Pipelines

MongoDB aggregation pipelines are made up of steps, each of which modifies a document. It is a multi-stage pipeline in which the documents are used as input to create the final set of documents.

What are Aggregation Pipeline Operators?

In the MongoDB aggregation framework, each command acts on a stream of documents, then performs some type of transformation, and finally returns the transformation result. If it is the last operator in the pipeline, the result will be returned to the user. Otherwise, the result is passed along to the next operator as input.

Basic MongoDB Aggregation Pipeline Stages

The basic pipeline stages offer:

  • Filters that behave like queries.
  • The document transformation changes the final document.
  • Providing a pipeline includes capabilities for grouping and sorting documents.

Key Stage Operators in MongoDB Aggregation Pipeline

  • $match: Filters documents in the aggregate pipeline according to the provided criteria.
  • $project: Reorganises documents by adding, removing, or renaming fields.
  • $group: Combines documents based on a defined key and conducts aggregation actions.
  • $sort: Sorts documents in the pipeline according to given fields.
  • $skip: Skips a given number of documents from the pipeline.
  • $limit: Sets the number of documents that can be transferred to the next stage of the pipeline.
  • $unwind: Deconstructs array fields in documents, resulting in one document for each element.
  • $out: Writes the aggregate pipeline's resulting documents to the given collection.

Common Aggregation Functions Used in the Group Stage

  • sum: Calculates the sum of numeric values for each page in the group.
  • count: Determines the total number of documents in each group.
  • avg: Calculates the average of all values entered from documents in each group.
  • min: Returns the minimum value from all documents in each group.
  • max: Determines the maximum value from all documents in each group.
  • first: Retrieves the first document in each collection.
  • last: Returns the last document in each group.

What is Map Reduce?

Map Reduce is used to aggregate results from massive amounts of data. Map reduction contains two primary functions: map to group all documents, and reduce to conduct operations on the grouped data.


Objective of MapReduce

MapReduce is a very powerful tool in the Aggregation Framework. It runs any type of JavaScript as a query language, allowing us to conduct advanced business logic. However, from a performance view, it is far too slow and should never be used with an active database.

What is Single-Purpose Aggregation?

Counting the number of documents or locating all distinct values in a document are two examples of when we utilize it when we need simple access to documents. It just gives access to the common aggregation process via the count(), distinct(), and estimatedDocumentCount() methods, limiting the pipeline's flexibility and capabilities.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this