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:累加器计算可变数量输入值的总和、最大值和最小值等聚合值。您可以在这些聚合管道阶段使用累加器:

Name名称Description描述
$accumulatorReturns 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.在版本5.0中的更改。 Available in the $setWindowFields stage.$setWindowFields阶段中可用。

$avg

Returns an average of numerical values. Ignores non-numeric values.返回数值的平均值。忽略非数值。

Changed in version 5.0.在版本5.0中的更改。 Available in the $setWindowFields stage.$setWindowFields阶段中可用。

$bottomReturns the bottom element within a group according to the specified sort order.根据指定的排序顺序返回组中的底部元素。

New in version 5.2.在版本5.2中新增。

Available in the $group and $setWindowFields stages.$group$setWindowFields阶段中可用。

$bottomNReturns an aggregation of the bottom n fields within a group, according to the specified sort order.根据指定的排序顺序,返回组内底部n个字段的聚合。

New in version 5.2.在版本5.2中新增。

Available in the $group and $setWindowFields stages.$group$setWindowFields阶段中可用。

$concatArrays

Returns a single array that combines the elements of two or more arrays.返回一个组合了两个或多个数组元素的数组。

New in version 8.1.在版本8.1中新增。

$count

Returns the number of documents in a group.返回组中的文档数。

Distinct from the $count pipeline stage.$count管道阶段不同。

New in version 5.0.在版本5.0中新增。 Available in the $group and $setWindowFields stages.$group$setWindowFields阶段中可用。

$first

Returns the result of an expression for the first document in a group.返回组中第一个文档的表达式结果。

Changed in version 5.0.在版本5.0中的更改。 Available in the $setWindowFields stage.$setWindowFields阶段中可用。

$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.返回组内前n个元素的聚合。只有当文档按定义的顺序排列时才有意义。与$firstN数组运算符不同。

New in version 5.2.在版本5.2中新增。 Available in the $group, expression and $setWindowFields stages.$group、表达式和$setWindowFields阶段中可用。

$last

Returns the result of an expression for the last document in a group.返回组中最后一个文档的表达式结果。

Changed in version 5.0.在版本5.0中的更改。 Available in the $setWindowFields stage.$setWindowFields阶段中可用。

$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.返回组内最后n个元素的聚合。只有当文档按定义的顺序排列时才有意义。与$lastN数组运算符不同。

New in version 5.2.在版本5.2中新增。 Available in the $group, expression and $setWindowFields stages.$group、表达式和$setWindowFields阶段中可用。

$max

Returns the highest expression value for each group.返回每个组的最高表达式值。

Changed in version 5.0.在版本5.0中的更改。 Available in the $setWindowFields stage.$setWindowFields阶段中可用。

$maxNReturns an aggregation of the n maximum valued elements in a group. 返回组中n个最大值元素的聚合。Distinct from the $maxN array operator.$maxN数组运算符不同。

New in version 5.2.在版本5.2中新增。

Available in $group, $setWindowFields and as an expression.$group$setWindowFields阶段中可用,用作表达式

$median

Returns an approximation of the median, the 50th percentile, as a scalar value.返回中值的近似值,即第50百分位,作为标量值。

New in version 7.0.在版本7.0中新增。

This operator is available as an accumulator in these stages:该操作器在以下阶段可用作累加器:

It is also available as an aggregation expression.它也可以作为聚合表达式使用。

$mergeObjectsReturns 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.在版本5.0中的更改。 Available in the $setWindowFields stage.$setWindowFields阶段中可用。

$minNReturns an aggregation of the n minimum valued elements in a group. Distinct from the $minN array operator.返回组中n个最小值元素的聚合。与$minN数组运算符不同。

New in version 5.2.在版本5.2中新增。

Available in $group, $setWindowFields and as an expression.可用于$group$setWindowFields表达式

$percentile

Returns an array of scalar values that correspond to specified percentile values.返回与指定百分位值对应的标量值数组。

New in version 7.0.在版本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.在版本5.0中的更改。 Available in the $setWindowFields stage.$setWindowFields阶段中可用。

$setUnion

Takes two or more arrays and returns an array containing the elements that appear in any input array.接受两个或多个数组,并返回一个包含出现在任何输入数组中的元素的数组。

New in version 8.1.在版本8.1中新增。

$stdDevPop

Returns the population standard deviation of the input values.返回输入值的总体标准偏差。

Changed in version 5.0.在版本5.0中的更改。 Available in the $setWindowFields stage.$setWindowFields阶段中可用。

$stdDevSamp

Returns the sample standard deviation of the input values.返回输入值的样本标准偏差。

Changed in version 5.0.在版本5.0中的更改。 Available in the $setWindowFields stage.$setWindowFields阶段中可用。

$sum

Returns a sum of numerical values. Ignores non-numeric values.返回数值之和。忽略非数值。

Changed in version 5.0.在版本5.0中的更改。 Available in the $setWindowFields stage.$setWindowFields阶段中可用。

$topReturns the top element within a group according to the specified sort order.根据指定的排序顺序返回组中的顶部元素。

New in version 5.2.在版本5.2中新增。

Available in the $group and $setWindowFields stages.$group$setWindowFields阶段中可用。

$topN

Returns an aggregation of the top n fields within a group, according to the specified sort order.根据指定的排序顺序,返回组内前n个字段的聚合。

New in version 5.2.在版本5.2中新增。

Available in the $group and $setWindowFields stages.$group$setWindowFields阶段中可用。