MongoDB Query Language (MQL) Accumulator Operators

Accumulator operators are used in aggregation pipelines to perform calculations on groups of documents. They take multiple input values and return a single output value, such as sums, averages, minimums, and maximums, enabling complex data analysis within your queries.

$avg

The `$avg` operator computes the average of numeric values across groups of documents or within defined windows.

$bottom

The `$bottom` operator sorts documents on one or more fields specified by the query and returns the last document matching the filtering criteria.

$bottomN

The `$bottomN` operator sorts documents on one or more fields specified by the query and returns the last N documents matching the filtering criteria.

$count

The `$count` operator is used to count the number of documents that match a specified query filter. The count operator is useful for summarizing data or generating counts for specific groupings.

$first

The `$first` operator sorts documents on one or more fields specified by the query and returns the first document matching the filtering criteria. If no sorting order is specified, the order is undefined.

$firstN

The `$firstN` operator returns the first N values in a group according to the group's sorting order. If no sorting order is specified, the order is undefined.

$last

The `$last` operator sorts documents on one or more fields specified by the query and returns the last document matching the filtering criteria.

$lastN

The `$lastN` accumulator operator returns the last N values in a group of documents for a specified expression. It's useful when you need to retrieve multiple final values from a sorted collection.

$max

The `$max` operator returns the maximum value of a set of input values. It can be used as an accumulator operator in aggregation or as a field update operator to set a field to a value only if the new value is greater than the current value.

$maxN

The `$maxN` operator is used to retrieve the top N values for a field based on a specified filtering criteria. It's useful for identifying the highest values in a dataset.

$median

The `$median` accumulator operator calculates the median value of a numeric field in a group of documents. It's useful for finding the middle value in a distribution of data.

$min

The `$min` operator is used to retrieve the minimum value for a specified field within aggregation stages like `$group`, `$bucket`, `$bucketAuto`, or `$setWindowFields`. It's particularly useful in summarizing data or finding the smallest value in a dataset. When used as a field update operator, it updates a field only if the new value is less than the current value.

$minN

The `$minN` operator is used to retrieve the bottom N values for a field based on a specified filtering criteria. It's useful for identifying the lowest values in a dataset.

$percentile

Calculates the percentile of numerical values that match a filtering criteria, useful for identifying statistical thresholds such as median or percentiles.

$stdDevPop

The `$stdDevPop` operator calculates the standard deviation of the specified values. The operator can only calculate the standard deviation of numeric values.

$stdDevSamp

The `$stdDevSamp` operator calculates the standard deviation by taking a specified sample of the values of a field. The standard deviation is calculated by taking a random sample of the specified size. If a precise standard deviation is needed, $stdDevPop must be used instead.

$sum

The `$sum` operator calculates the sum of numeric values of a field or expression that match a filtering criteria.

$top

The `$top` operator sorts documents on one or more fields specified by the query and returns the first document matching the filtering criteria. It combines sorting and selection in a single operation.

$topN

The `$topN` operator sorts documents on one or more fields specified by the query and returns the first N documents matching the filtering criteria. It extends the functionality of `$top` by allowing you to retrieve multiple top elements.