For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Accumulators

Accumulators calculate aggregated values like sums, maxima, and minima across a variable number of input values. You can use accumulators in these aggregation pipeline stages:

Name
Description

Returns the result of a user-defined accumulator function.

Returns an array of unique expression values for each group. Order of the array elements is undefined.

Changed in version 5.0. :

Available in the $setWindowFields stage.

Returns an average of numerical values. Ignores non-numeric values.

Changed in version 5.0. :

Available in the $setWindowFields stage.

Returns the bottom element within a group according to the specified sort order.

New in version 5.2. :

Available in the $group and $setWindowFields stages.

Returns an aggregation of the bottom n fields within a group, according to the specified sort order.

New in version 5.2. :

Available in the $group and $setWindowFields stages.

Returns the number of documents in a group.

Distinct from the $count pipeline stage.

New in version 5.0. :

Available in the $group and $setWindowFields stages.

Returns the result of an expression for the first document in a group.

Changed in version 5.0. :

Available in the $setWindowFields stage.

Returns an aggregation of the first n elements within a group. Only meaningful when documents are in a defined order. Distinct from the $firstN array operator.

New in version 5.2. :

Available in the $group, expression and $setWindowFields stages.

Returns the result of an expression for the last document in a group.

Changed in version 5.0. :

Available in the $setWindowFields stage.

Returns an aggregation of the last n elements within a group. Only meaningful when documents are in a defined order. Distinct from the $lastN array operator.

New in version 5.2. :

Available in the $group, expression and $setWindowFields stages.

Returns the highest expression value for each group.

Changed in version 5.0. :

Available in the $setWindowFields stage.

Returns an aggregation of the n maximum valued elements in a group. Distinct from the $maxN array operator.

New in version 5.2. :

Available in $group, $setWindowFields and as an expression.

Returns an approximation of the median, the 50th percentile, as a scalar value.

New in version 7.0. :

This operator is available as an accumulator in these stages:

It is also available as an aggregation expression.

Returns a document created by combining the input documents for each group.

Returns the lowest expression value for each group.

Changed in version 5.0. :

Available in the $setWindowFields stage.

Returns an aggregation of the n minimum valued elements in a group. Distinct from the $minN array operator.

New in version 5.2. :

Available in $group, $setWindowFields and as an expression.

Returns an array of scalar values that correspond to specified percentile values.

New in version 7.0. :

This operator is available as an accumulator in these stages:

It is also available as an aggregation expression.

Returns an array of expression values for documents in each group.

Changed in version 5.0. :

Available in the $setWindowFields stage.

Returns the population standard deviation of the input values.

Changed in version 5.0. :

Available in the $setWindowFields stage.

Returns the sample standard deviation of the input values.

Changed in version 5.0. :

Available in the $setWindowFields stage.

Returns a sum of numerical values. Ignores non-numeric values.

Changed in version 5.0. :

Available in the $setWindowFields stage.

Returns the top element within a group according to the specified sort order.

New in version 5.2. :

Available in the $group and $setWindowFields stages.

Returns an aggregation of the top n fields within a group, according to the specified sort order.

New in version 5.2. :

Available in the $group and $setWindowFields stages.