Aggregation in MongoDB: Aggregate Pipeline and Map Reduce

An Overview of Aggregation in MongoDB:

Authentication in MongoDB was explained in detail in our previous tutorial in this Detailed MongoDB training series.

In this tutorial, we will learn about Aggregation in MongoDB.

In simple words, aggregation means to combine different resource of information and provide the most authentic record. In MongoDB, it is the process to validate information from a different collection and in return provide a single record.

Aggregation in MongoDB

Various operations are performed on the collected data to extract only the valid information.

In MongoDB, three types of aggregation are available as shown below:

  1. Aggregation Pipeline
  2. Map Reduce
  3. Single Purpose Aggregation

Aggregation Pipeline

Aggregation Framework in MongoDB is developed on the concept of data processing pipelines. In this pipeline, a set of various functions are applied on a document which is entered in the pipeline to aggregate the final result.

Basically, two operations are performed on any document within the pipeline. First, the records are filtered just like how queries are performed and in the second phase, the transformation of the document happens to change its type for output purpose.

On the other hand, pipeline operations are also used for sorting, grouping, merging & aggregation of arrays and arrays of the document. Somehow pipelines can also be used to summarize the content or to calculate the average and concatenation of record.

Code

db.orders.aggregate([{$match:{status:"A"}},{$group:{_id:"$cust_id", total:{$sum:"$amount"}}}])

Figure 1: In Mongo Shell

Aggregation Pipeline in mongodb

Figure 2: In Robo 3T

Aggregation Pipeline in robo 3t

Figure 3

Aggregation - Robo 3T

Map Reduce

MongoDB also provides the Map Reduce feature for aggregation purposes. Generally, there are two phases of Map Reduce. In the first phase, each document is processed and emits common and redundant part of the document to pass a unique record for the next phase.

In the second phase, all the unique parts get together and aggregate to produce a single result. Map Reduce also provide sorting, filtering, and document modification.

Code

db.orders.mapReduce(function(){emit(this.cust_id,this.amount);}, 
function(key,values){return Array.sum(values)},{query:{status:"A"},out: "order_totals"}).find()

Figure 4: In Mongo Shell

Map Reduce in mongo db

Figure 5: In Robo 3T

Map Reduce in Robo 3t

Figure 6

Map Reduce

Single Purpose Aggregation

In the single purpose aggregation, only one filter is applied to calculate the result. In simple words, if we have to aggregate a whole collection based on one filter, then we have to use single-purpose aggregation operations.

In MongoDB we have three kinds of aggregation operations for a single filtration:

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

All of the above operations are used for single purpose aggregation. These operations provide a simple access control upon the common processes of aggregation. These operations will not provide extensive filtration and sorting just like aggregation pipeline and Map Reduce.

Code

db.orders.distinct("cust_id")

Figure 7: In Mongo Shell

Single Purpose Aggregation in Mongodb

Figure 8: In Robo 3T

Single Purpose Aggregation in Robo 3t

Figure 9

Single Purpose Aggregation

Conclusion

Aggregation is the process of collecting information to provide the average result. It is also used in analytical purposes. In this tutorial, we have learned about the three types of aggregation that are available in MongoDB to process information.

MongoDB also provides us the map reduce method, which is used to aggregate huge information. Map Reduce is mostly used for big data. All of these aggregation methodologies are used based upon the condition of the records and the resultant values.


In our upcoming tutorial, we will learn about Projection in MongoDB in detail.

PREV Tutorial | NEXT Tutorial