On this page本页内容
$sum
¶Calculates and returns the sum of numeric values. 计算并返回数值之和。$sum
ignores non-numeric values.忽略非数值。
$sum
is available in the following stages:在以下阶段提供:
$group
$project
$addFields
$set
$replaceRoot
$replaceWith
$match
stage that includes an $expr
expression$expr
表达式的$match
阶段,In MongoDB 3.2 and earlier, 在MongoDB 3.2及更早版本中,$sum
is available in the $group
stage only.$sum
仅在$group
阶段可用。
When used in the 在$group
stage, $sum
has the following syntax and returns the collective sum of all the numeric values that result from applying a specified expression to each document in a group of documents that share the same group by key:$group
阶段中使用时,$sum
具有以下语法,并返回将指定表达式应用于共享同一分组键的文档组中的每个文档所产生的所有数值的总和:
When used in the other supported stages, 在其他受支持的阶段中使用时,$sum
returns the sum of the specified expression or list of expressions for each document and has one of two syntaxes:$sum
返回每个文档的指定表达式或表达式列表的总和,并具有以下两种语法之一:
$sum
$sum
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
If used on a field that contains both numeric and non-numeric values, 如果在同时包含数值和非数值的字段上使用,$sum
ignores the non-numeric values and returns the sum of the numeric values.$sum
忽略非数值并返回数值之和。
If used on a field that does not exist in any document in the collection, 如果在集合中的任何文档中都不存在的字段上使用,$sum
returns 0
for that field.$sum
会为该字段返回0
。
If all operands are non-numeric, 如果所有操作数都是非数字的,$sum
returns 0
.$sum
返回0
。
Field Values | ||
---|---|---|
{ $sum : <field> } |
Numeric |
Sum of Values |
{ $sum : <field> } |
Numeric and Non-Numeric |
Sum of Numeric Values |
{ $sum : <field> } |
Non-Numeric or Non-Existent |
0 |
In the 在$group
stage, if the expression resolves to an array, $sum
treats the operand as a non-numerical value.$group
阶段,如果表达式解析为数组,$sum
将操作数视为非数值。
In the other supported stages:在其他受支持的阶段:
$sum
traverses into the array to operate on the numerical elements of the array to return a single value.$sum
遍历数组,对数组的数字元素进行操作,以返回单个值。$sum
does not traverse into the array but instead treats the array as a non-numerical value.$sum
不会遍历数组,而是将数组视为非数值。$group
Stage$group
阶段使用¶Consider a 考虑一个sales
collection with the following documents:sales
集合带有以下文档:
Grouping the documents by the day and the year of the 按照date
field, the following operation uses the $sum
accumulator to compute the total amount and the count for each group of documents.date
字段的日期和年份对文档进行分组,下面的操作使用$sum
累加器计算每组文档的总金额和计数。
The operation returns the following results:操作返回以下结果:
Using 对不存在的字段使用$sum
on a non-existent field returns a value of 0
. $sum
返回值0
。The following operation attempts to 以下操作尝试对$sum
on qty
:qty
进行$sum
:
The operation returns:操作返回:
$project
Stage$project
阶段使用¶A collection students
contains the following documents:students
集合包括以下文件:
The following example uses the 以下示例使用$sum
in the $project
stage to calculate the total quiz scores, the total lab scores, and the total of the final and the midterm:$project
阶段中的$sum
计算测验总分、实验室总分以及期末和期中考试的总分:
The operation results in the following documents:该操作将生成以下文档:
In the other supported stages:在其他受支持的阶段:
$sum
traverses into the array to operate on the numerical elements of the array to return a single value.$sum
遍历数组,对数组的数字元素进行操作,以返回单个值。$sum
does not traverse into the array but instead treats the array as a non-numerical value.$sum
不会遍历数组,而是将数组视为非数值。