$max (aggregation)

On this page本页内容

Definition定义

$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 (Available starting in MongoDB 3.4)(从MongoDB 3.4开始提供)
  • $set (Available starting in MongoDB 4.2)(从MongoDB 4.2开始提供)
  • $replaceRoot (Available starting in MongoDB 3.4)(从MongoDB 3.4开始提供)
  • $replaceWith (Available starting in MongoDB 4.2)(从MongoDB 4.2开始提供)
  • $match stage that includes an $expr expression包含一个$expr表达式的$match阶段

In MongoDB 3.2 and earlier, $max is available in the $group stage only.在MongoDB 3.2及更早版本中,$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键的一组文档中的每个文档所产生的最大值:

{ $max: <expression> }

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 has one specified expression as its operand:具有一个指定的表达式作为其操作数:

    { $max: <expression> }
  • $max has a list of specified expressions as its operand:将指定表达式的列表作为其操作数:

    { $max: [ <expression1>, <expression2> ... ]  }

For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

Null or Missing Values空值或缺少值

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作为最大值。

Array Operand数组操作数

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:在其他受支持的阶段:

  • With a single expression as its operand, if the expression resolves to an array, $max traverses into the array to operate on the numerical elements of the array to return a single value.使用单个表达式作为其操作数,如果表达式解析为数组,$max遍历数组,对数组的数字元素进行操作,以返回单个值。
  • With a list of expressions as its operand, if any of the expressions resolves to an array, $max does not traverse into the array but instead treats the array as a non-numerical value.使用表达式列表作为其操作数,如果任何表达式解析为数组,$max不会遍历数组,而是将数组视为非数值。

Examples示例

Use in $group Stage$group阶段中使用

Consider a sales collection with the following documents:考虑一个sales集合带有以下文档:

{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") }
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:05:00Z") }

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累加器计算每组文档的最大总金额和最大数量。

db.sales.aggregate(
   [
     {
       $group:
         {
           _id: "$item",
           maxTotalAmount: { $max: { $multiply: [ "$price", "$quantity" ] } },
           maxQuantity: { $max: "$quantity" }
         }
     }
   ]
)

The operation returns the following results:操作返回以下结果:

{ "_id" : "xyz", "maxTotalAmount" : 50, "maxQuantity" : 10 }
{ "_id" : "jkl", "maxTotalAmount" : 20, "maxQuantity" : 1 }
{ "_id" : "abc", "maxTotalAmount" : 100, "maxQuantity" : 10 }

Use in $project Stage$project阶段中使用

A collection students contains the following documents:students集合包括以下文档:

{ "_id": 1, "quizzes": [ 10, 6, 7 ], "labs": [ 5, 8 ], "final": 80, "midterm": 75 }
{ "_id": 2, "quizzes": [ 9, 10 ], "labs": [ 8, 8 ], "final": 95, "midterm": 80 }
{ "_id": 3, "quizzes": [ 4, 5, 5 ], "labs": [ 6, 5 ], "final": 78, "midterm": 70 }

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来计算测验的最高分数、实验室的最高分数以及期末和期中的最高分数:

db.students.aggregate([
   { $project: { quizMax: { $max: "$quizzes"}, labMax: { $max: "$labs" }, examMax: { $max: [ "$final", "$midterm" ] } } }
])

The operation results in the following documents:该操作将生成以下文档:

{ "_id" : 1, "quizMax" : 10, "labMax" : 8, "examMax" : 80 }
{ "_id" : 2, "quizMax" : 10, "labMax" : 8, "examMax" : 95 }
{ "_id" : 3, "quizMax" : 5, "labMax" : 6, "examMax" : 78 }

In the other supported stages:在其他受支持的阶段:

  • With a single expression as its operand, if the expression resolves to an array, $max traverses into the array to operate on the numerical elements of the array to return a single value.使用单个表达式作为其操作数,如果表达式解析为数组,$max遍历数组,对数组的数字元素进行操作,以返回单个值。
  • With a list of expressions as its operand, if any of the expressions resolves to an array, $max does not traverse into the array but instead treats the array as a non-numerical value.使用表达式列表作为其操作数,如果任何表达式解析为数组,$max不会遍历数组,而是将数组视为非数值。