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.
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
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.

Returns an average of numerical values.

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

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

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

New in version 8.1.

Returns the number of documents in a group.

Distinct from the $count pipeline stage.

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

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.

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

Returns the result of evaluating a specified expression over the last n elements in a group.

Returns the highest expression value for each group.

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

Returns an approximation of the median (50th percentile) value of documents in a group.

New in version 7.0.

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

Returns the lowest expression value for each group.

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

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

New in version 7.0.

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

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

New in version 8.1.

Returns the population standard deviation of values from documents in a group.

Returns the sample standard deviation of values from documents in a group.

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

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

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

Rate this page