MongoDB Aggregation Framework
Aggregation framework is just another way of querying data in mongodb.
It enables you to group data, perform calculations, reshape data etc.
It works based on the concept called, pipeline. Pipeline consists of,
Composition of stages.
Configurable transformation.
Stages can be arranged in multiple ways (with few eceptions).
Pipeline are always an array of operations of one or more stages.
Stages are composed of one or more aggregation operators or expressions.
The expression may take single or an array of arguments i.e expression dependent.
Syntax
Example
In the above example, we prepare an aggregation pipeline when we use aggregate function.
The first aggregation operator in pipeline is "$match" operation and then "$project" agggegation operation.
The aggregate function takes array as parameter. This array represents order of aggregation operation in pipeline as per which data need to be modified or organized.
The above query helps in filtering documents based on the match of field value and then only projecting based on field specified.
Read more about it here.
Some aggregation terms
Field Path
Example
"$fieldName"
System Variable
Example
"$$CURRENT"
All uppercase variables are system variable.
User Variable
Example
"$$foo"
All lowercase variables are user variable.
Last updated