What is Aggregation Pipeline?
An aggregation pipeline consists of one or more stages that process documents:
➡ Each stage performs an operation on the input documents. For example, a stage can filter documents, group documents, and calculate values.
➡ The documents that are output from a stage are passed to the next stage.
➡ An aggregation pipeline can return results for groups of documents. For example, return the total, average, maximum, and minimum values.
MongoDB aggregation tutorial video
What are stages?
In the db.collection.aggregate() method and db.aggregate() method, pipeline stages appear in an array. Documents pass through the stages in sequence.
All except the $out, $merge, $geoNear, and $changeStream stages can appear multiple times in a pipeline
Syntax:
db.collection.aggregate( [ { <stage> }, ... ] );
Types of Stages:
$project – Shows some specific fields
$match – filters those documents we need to work with, those that fit our needs
$group – grouping the fields to perform aggregation
$sort – sorts the resulting documents the way we require
$limit – limits the documents to display