- Aggregation Operations >
- Aggregation Pipeline
Aggregation Pipeline¶
On this page
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.
Starting in MongoDB 4.2, you can also update documents with an aggregation pipeline. See Updates with Aggregation Pipeline.
Complete Aggregation Pipeline Examples¶
This section shows aggregation pipeline examples that use the following
pizza orders
collection:
Calculate Total Order Quantity¶
The following aggregation pipeline example contains two stages and returns the total order quantity of medium size pizzas grouped by pizza name:
The $match
stage:
- Filters the pizza order documents to pizzas with a
size
ofmedium
. - Passes the remaining documents to the
$group
stage.
The $group
stage:
- Groups the remaining documents by pizza
name
. - Uses
$sum
to calculate the total orderquantity
for each pizzaname
. The total is stored in thetotalQuantity
field returned by the aggregation pipeline.
Example output:
Calculate Total Order Value and Average Order Quantity¶
The following example calculates the total pizza order value and average order quantity between two dates:
The $match
stage:
- Filters the pizza order documents to those in a date range specified
using
$gte
and$lt
. - Passes the remaining documents to the
$group
stage.
The $group
stage:
- Groups the documents by date using
$dateToString
. - For each group, calculates:
- Passes the grouped documents to the
$sort
stage.
The $sort
stage:
- Sorts the documents by the total order value for each group in
descending order (
-1
). - Returns the sorted documents.
Example output:
Additional Aggregation Pipeline Stage Details¶
An aggregation pipeline consists of one or more stages that process documents:
- A stage does not have to output one document for every input document. For example, some stages may produce new documents or filter out documents.
- The same stage can appear multiple times in the pipeline with these
stage exceptions:
$out
,$merge
, and$geoNear
. - To calculate averages and perform other calculations in a stage, use aggregation expressions that specify aggregation operators. You will learn more about aggregation expressions in the next section.
For all aggregation stages, see Aggregation Pipeline Stages.
Aggregation Pipeline Expressions¶
Some aggregation pipeline stages accept an aggregation expression, which:
- Specifies the transformation to apply to the current stage’s input documents.
- Transform the documents in memory.
- Can specify aggregation expression operators to calculate values.
- Can contain additional nested aggregation expressions.
Starting in MongoDB 4.4, you can use the $accumulator
and
$function
aggregation operators to define custom
aggregation expressions in JavaScript.
For all aggregation expressions, see Expressions.
Run an Aggregation Pipeline¶
To run an aggregation pipeline, use:
Update Documents Using an Aggregation Pipeline¶
To update documents with an aggregation pipeline, use:
Command | mongosh Methods |
---|---|
findAndModify |
|
update |
Other Considerations¶
Aggregation Pipeline Limitations¶
An aggregation pipeline has limitations on the value types and the result size. See Aggregation Pipeline Limits.
Aggregation Pipelines and Sharded Collections¶
An aggregation pipeline supports operations on sharded collections. See Aggregation Pipeline and Sharded Collections.
Aggregation Pipelines as an Alternative to Map-Reduce¶
Starting in MongoDB 5.0, map-reduce is deprecated:
- Instead of map-reduce, you should use an aggregation pipeline. Aggregation pipelines provide better performance and usability than map-reduce.
- You can rewrite map-reduce operations using aggregation
pipeline stages, such as
$group
,$merge
, and others. - For map-reduce operations that require custom functionality, you can
use the
$accumulator
and$function
aggregation operators, available starting in version 4.4. You can use those operators to define custom aggregation expressions in JavaScript.
For examples of aggregation pipeline alternatives to map-reduce, see: