Aggregation operations process data records and return computed results. Aggregation operations group values from multiple documents together and can perform a variety of operations on the grouped data to return a single result. The key element in aggregation is called the pipeline. It also helps us in performing a few operations like min, max, sum, etc. MongoDB provides three ways to perform aggregation: a)Aggregation Pipeline; b) Map-Reduce Function; c) Single Purpose Aggregation Methods
MongoDB’s aggregation framework is modeled on the concept of data processing pipelines. Documents enter a multi-stage pipeline that transforms the documents into an aggregated result.
A sequence of data aggregation operations or stages. The pipeline is an array.The Aggregation Stages are namely: $count; $group; $limit; $lookup; $match; $merge; $sort; $project; $unwind; $unset.
The pipeline definition is :
var pipeline = [
{….},
{….},
{….},
]
Example:
var pipeline = [
{$group:{_id:”$name”}},
{$sort:{“birth_year”:1}},
{$limit:5},
]
db.students.aggregate(pipeline)