Navigation

Group Accumulator Operators

Available for use in the $group stage, accumulators are operators that maintain their state (e.g. totals, maximums, minimums, and related data) as documents progress through the pipeline.

When used as accumulators in the $group stage, these operators take as input a single expression, evaluating the expression once for each input document, and maintain their stage for the group of documents that share the same group key.

Changed in version 3.2: Some operators that are available as accumulators for the $group stage are also available for use in the $project stage but not as accumulators. When used in the $project stage, these operators do not maintain their state and can take as input either a single argument or multiple arguments.

Note

For details on specific operator, including syntax and examples, click on the specific operator to go to its reference page.

Name Description
$sum

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

Changed in version 3.2: Available in both $group and $project stages.

$avg

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

Changed in version 3.2: Available in both $group and $project stages.

$first

Returns a value from the first document for each group. Order is only defined if the documents are in a defined order.

Available in $group stage only.

$last

Returns a value from the last document for each group. Order is only defined if the documents are in a defined order.

Available in $group stage only.

$max

Returns the highest expression value for each group.

Changed in version 3.2: Available in both $group and $project stages.

$min

Returns the lowest expression value for each group.

Changed in version 3.2: Available in both $group and $project stages.

$push

Returns an array of expression values for each group.

Available in $group stage only.

$addToSet

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

Available in $group stage only.

$stdDevPop

Returns the population standard deviation of the input values.

Changed in version 3.2: Available in both $group and $project stages.

$stdDevSamp

Returns the sample standard deviation of the input values.

Changed in version 3.2: Available in both $group and $project stages.