On this page本页内容
Changed in version 5.0.在版本5.0中更改。
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 these stages:可在以下阶段使用:
$addFields (Available starting in MongoDB 3.4)$bucket$bucketAuto$group$match$expr expression$expr表达式的阶段$project$replaceRoot$replaceWith$set$setWindowFieldsIn MongoDB 3.2 and earlier, 在MongoDB 3.2及更早版本中,$max is available in the $group stage only.$max仅在$group阶段可用。
When used in the 在$bucket, $bucketAuto, $group, and $setWindowFields stages, $max has this syntax:$bucket、$bucketAuto、$group和$setWindowFields阶段中使用时,$max具有以下语法:
{ $max: <expression> }
When used in other supported stages, 在其他受支持的阶段中使用时,$max 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.有关表达式的详细信息,请参阅表达式。
Null或缺少值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操作的某些(但不是全部)文档的字段值为空或缺少字段,则$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 and $setWindowFields stages, if the expression resolves to an array, $max does not traverse the array and compares the array as a whole.$group和$setWindowFields阶段中,如果表达式解析为数组,则$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集合:
{ "_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 }
$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 }
$setWindowFields Stage$setWindowFields阶段中使用New in version 5.0.在版本5.0中新增。
Create a 创建包含加利福尼亚州(cakeSales collection that contains cake sales in the states of California (CA) and Washington (WA):CA)和华盛顿州(WA)蛋糕销售的cakeSales集合:
db.cakeSales.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120 },
{ _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140 },
{ _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145 },
{ _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104 },
{ _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162 },
{ _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134 }
] )
This example uses 本例使用$max in the $setWindowFields stage to output the maximum quantity of cake sales for each state:$setWindowFields阶段中的$max输出每个州的最大蛋糕销售量quantity:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
maximumQuantityForState: {
$max: "$quantity",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state" state. state对集合中的文档进行分区。CA and WA.CA和WA分区。sortBy: { orderDate: 1 } orderDate in ascending order (1), so the earliest orderDate is first.orderDate以升序(1)对每个分区中的文档进行排序,因此最早的orderDate居第一。output sets the 使用在文档窗口中运行的maximumQuantityForState field to the maximum quantity value using $max that is run in a documents window.$max将maximumQuantityForState字段设置为最大quantity值。
The window contains documents between an 该窗口包含输出中处于unbounded lower limit and the current document in the output. unbounded下限和current文档之间的文档。This means 这意味着$max returns the maximum quantity for the documents between the beginning of the partition and the current document.$max返回分区开始处和当前文档之间的文档的最大数量。
In this output, the maximum 在此输出中,quantity for CA and WA is shown in the maximumQuantityForState field:CA和WA的最大quantity显示在maximumQuantityForState字段中:
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "maximumQuantityForState" : 162 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "maximumQuantityForState" : 162 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "maximumQuantityForState" : 162 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "maximumQuantityForState" : 134 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "maximumQuantityForState" : 134 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "maximumQuantityForState" : 140 }