Aggregation pipeline in mongoDB

Ayushi Bansal January 24, 2020

Aggregation pipeline in mongoDB

Aggregation

Aggregation simply means collection of related items of content which are brought together so that they can be displayed as per the requirements. It literally means processing data records in a logical manner to return the desired output.

Aggregation Pipeline

It works on the concept of data processing through pipelines. The documents enter the multi-stage pipeline that transforms the data into aggregated results.

Stages of Pipeline

First Stage – The $match stage filters the documents by the field name and passes data to the next stage.

Second Stage – The $group stage groups the documents by the field name to show the desired output.

The aggregate() method in mongoDB

We use the aggregate method in mongoDB as mentioned below :

db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION) 

Example:

Output:



Pipeline operators in mongoDB :

1) $projectThis function passes only the specified fields to the next stages of the pipeline.                                             

2) $matchThis function filters the data according to the specified condition . It passes only the filtered data to next stages of pipeline.

3) $unwindIt is used to output a document for each element of an array.

4) $groupAggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result.

Aggregation pipeline can be used as illustrated below:

 

Output:


If you have any queries or doubts about this topic please feel free to contact us. We are here to help you!

Lets’s Talk

About your ideas and concept