Aggregation in MongoDB

Vaibhav Sharma
3 min readOct 16, 2018
Aggregation by pipeline

Today we are discussed about Aggregation in MongoDB. Aggregation is the process of grouping the multiple documents and perform any operation and getting the single result. In this medium story, we will take a look at how to create the basic transformation of documents using aggregations.

There are 3 ways to perform aggregation on MongoDB documents:

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

Basic Syntax :

db.collection_name.aggregate(AGGREGATION_OPERATIONS)

AGGREGATION PIPELINE :

MongoDB aggregation framework using the data-processing pipeline. MongoDB documents are processed by aggregation pipeline and give the aggregate result. The pipeline method using the MongoDB native methods like a $match, $group, $sort etc.

db.collection_name.aggregate([...AGGREGATION_OPERATIONS...])

For working with aggregation, we need to insert some data in the database.

Insert data by mongodb.insertMany( )
Insert some data

If you run above command in Studio 3T, then the result like this:

Inserted Data

After inserting the above data, we have a requirement to find a total number of employees living in New Delhi. There are 2 ways of finding:

  1. By the use of find() and calculate length of array.
  2. By the use of aggregate()

The first method has two steps first is a query to the database and second is find the length of resulting array. But second method(aggregate) gives the end result by the use of a query. By the use of below MongoDB query, we will find the final result.

This query has 2 stages:

  1. Match
  2. Group
Aggregation Pipeline

In the first stage, we find all the document by $match who having “zip”: 110001 and in the second stage we group them by the $group and find no_of_employee by the $sum.

Result of above query

MAP — REDUCE METHOD :

This method is based on javascript’s map-reduce method. In this method, there are 2 phases the first phase is the map that processes each document and emits one or more objects for each input document, and the second phase is the reduce that combines the output of the map operation. Map-Reduce is less efficient and more complex than the aggregate pipeline method.

Mongodb.mapReduce( ) syntax

In the above query, we can use mongodb.mapReduce(). It’s having two functions and one object. The first function is for Map stage and another function is for Reduce stage and an object having a query and out ( the location of the map-reduce query result). Below is the result of above query:

If you find the document of above aggregation then we will add find query at the end of above query.

Result of above query is :

Result of above query

SINGLE PURPOSE AGGREGATION METHODS :

There are some methods for aggregate documents. These functions are:

  1. estimatedDocumentCount( )
  2. count( )
  3. distinct( )

These functions lack the flexibility and capabilities of MAP-REDUCE and AGGREGATE PIPELINE.

distinct query
Result of above query

Conclusion :

Above medium story gives a brief look of Aggregation in MongoDB. If you have any doubts then mail me on vsvaibhav2016@gmail.com.

--

--