On this page本页内容
$max¶Returns the maximum value. 返回最大值$max compares both value and type, using the specified BSON comparison order for values of different types.$max对不同类型的值使用指定的BSON比较顺序来比较值和类型。
$max 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及更早版本中,$max is available in the $group stage only.$max仅在$group阶段可用。
When used in the 在$group stage, $max has the following syntax and returns the maximum value that results from applying an expression to each document in a group of documents that share the same group by key:$group阶段中使用时,$max具有以下语法,并返回将表达式应用于共享同一group by键的一组文档中的每个文档所产生的最大值:
When used in the other supported stages, 在其他受支持的阶段中使用时,$max returns the maximum of the specified expression or list of expressions for each document and has one of two syntaxes:$max返回每个文档的指定表达式或表达式列表的最大值,并具有以下两种语法之一:
$max$maxFor more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
If some, but not all, documents for the 如果$max operation have either a null value for the field or are missing the field, the $max operator only considers the non-null and the non-missing values for the field.$max操作的部分文档(但不是全部文档)的字段值为null或缺少字段,则$max运算符只考虑字段的非空值和非缺少值。
If all documents for the 如果$max operation have null value for the field or are missing the field, the $max operator returns null for the maximum value.$max操作的所有文档的字段值都为null或缺少该字段,则$max运算符将返回null作为最大值。
In the 在$group stage, if the expression resolves to an array, $max does not traverse the array and compares the array as a whole.$group阶段,如果表达式解析为数组,$max不会遍历数组并将数组作为一个整体进行比较。
In the other supported stages:在其他受支持的阶段:
$max traverses into the array to operate on the numerical elements of the array to return a single value.$max遍历数组,对数组的数字元素进行操作,以返回单个值。$max does not traverse into the array but instead treats the array as a non-numerical value.$max不会遍历数组,而是将数组视为非数值。$group Stage$group阶段中使用¶Consider a 考虑一个sales collection with the following documents:sales集合带有以下文档:
Grouping the documents by the 根据item field, the following operation uses the $max accumulator to compute the maximum total amount and maximum quantity for each group of documents.item字段对文档进行分组,以下操作使用$max累加器计算每组文档的最大总金额和最大数量。
The operation returns the following results:操作返回以下结果:
$project Stage$project阶段中使用¶A collection students contains the following documents:students集合包括以下文档:
The following example uses the 以下示例使用$max in the $project stage to calculate the maximum quiz scores, the maximum lab scores, and the maximum of the final and the midterm:$project阶段中的$max来计算测验的最高分数、实验室的最高分数以及期末和期中的最高分数:
The operation results in the following documents:该操作将生成以下文档:
In the other supported stages:在其他受支持的阶段:
$max traverses into the array to operate on the numerical elements of the array to return a single value.$max遍历数组,对数组的数字元素进行操作,以返回单个值。$max does not traverse into the array but instead treats the array as a non-numerical value.$max不会遍历数组,而是将数组视为非数值。