Docs Menu
Docs Home
/
Database Manual
/

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.

You can update documents with an aggregation pipeline if you use the stages shown in Updates with Aggregation Pipeline.

Note

You can run aggregation pipelines in the UI for deployments hosted in MongoDB Atlas.

When you run aggregation pipelines on MongoDB Atlas deployments in the MongoDB Atlas UI, you can preview the results at each stage.

The Complete Aggregation Pipeline Tutorials section contains complete tutorials that provide detailed explanations of common aggregation tasks in a step-by-step format. The tutorials include examples for MongoDB Shell and each of the official MongoDB drivers.

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 Stages.

Some aggregation pipeline stages accept expressions. Operators calculate values based on input expressions.

In the MongoDB Query Language, you can build expressions from the following components:

Component
Example

Constants

3

Operators

Field path expressions

"$<path.to.field>"

For example, { $add: [ 3, "$inventory.total" ] } is an expression consisting of the $add operator and two input expressions:

The expression returns the result of adding 3 to the value at path inventory.total of the input document.

Field path expressions are used to access fields in input documents. To specify a field path, prefix the field name or the dotted field path (if the field is in an embedded document) with a dollar sign $. For example, "$user" to specify the field path for the user field or "$user.name" to specify the field path to the embedded "user.name" field.

"$<field>" is equivalent to "$$CURRENT.<field>" where the CURRENT is a system variable that defaults to the root of the current object, unless stated otherwise in specific stages.

For more information and examples, see Field Paths.

To run an aggregation pipeline, use:

To update documents with an aggregation pipeline, use:

An aggregation pipeline has limitations on the value types and the result size. See Aggregation Pipeline Limits.

An aggregation pipeline supports operations on sharded collections. See Aggregation Pipeline and Sharded Collections.

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. You can use those operators to define custom aggregation expressions in JavaScript.

For examples of aggregation pipeline alternatives to map-reduce, see:

To learn more about aggregation pipelines, see:

Back

Aggregation Operations

Earn a Skill Badge

Master "Fundamentals of Data Transformation" for free!

Learn more

On this page