翻译或纠错本页面

Group Accumulator Operators

在 3.2 版更改: Some accumulators are now available in the $project stage. In previous versions of MongoDB , accumulators are available only for the $group stage.

Accumulators, when used in the $group stage, maintain their state (e.g. totals, maximums, minimums, and related data) as documents progress through the pipeline.

When used in the $group stage, accumulators 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.

When used in the $project stage, the accumulators do not maintain their state. When used in the $project stage, accumulators take as input either a single argument or multiple arguments.

注解

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.

在 3.2 版更改: Available in both $group and $project stages.

$avg

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

在 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.

在 3.2 版更改: Available in both $group and $project stages.

$min

Returns the lowest expression value for each group.

在 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.

在 3.2 版更改: Available in both $group and $project stages.

$stdDevSamp

Returns the sample standard deviation of the input values.

在 3.2 版更改: Available in both $group and $project stages.