Database Manual / Reference / Query Language

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:

NameDescription

$accumulator

Returns the result of a user-defined accumulator function.

$addToSet

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.

$avg

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

Changed in version 5.0: Available in the $setWindowFields stage.

$bottom

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.

$bottomN

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.

$concatArrays

Returns a single array that combines the elements of two or more arrays.

New in version 8.1.

$count

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.

$first

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

Changed in version 5.0: Available in the $setWindowFields stage.

$firstN

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.

$last

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

Changed in version 5.0: Available in the $setWindowFields stage.

$lastN

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.

$max

Returns the highest expression value for each group.

Changed in version 5.0: Available in the $setWindowFields stage.

$maxN

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.

$median

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.

$mergeObjects

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

$min

Returns the lowest expression value for each group.

Changed in version 5.0: Available in the $setWindowFields stage.

$minN

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.

$percentile

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.

$push

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

Changed in version 5.0: Available in the $setWindowFields stage.

$setUnion

Takes two or more arrays and returns an array containing the elements that appear in any input array.

New in version 8.1.

$stdDevPop

Returns the population standard deviation of the input values.

Changed in version 5.0: Available in the $setWindowFields stage.

$stdDevSamp

Returns the sample standard deviation of the input values.

Changed in version 5.0: Available in the $setWindowFields stage.

$sum

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

Changed in version 5.0: Available in the $setWindowFields stage.

$top

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.

$topN

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.