文章目录
$max
聚合运算符用于返回最大值。 $max
对于不同的类型的值使用BSON的比较顺序。
$max
可以用于下面的这些阶段:
$addFields
$bucket
$bucketAuto
$group
$match
$project
$replaceRoot
$replaceWith
$set
$setWindowFields
语法
当用于$bucket
、$bucketAuto
、$group
、$setWindowFields
阶段时,$max
使用下面的语法:
js
{ $max: <expression> }
在用于其他阶段时,使用的语法如下:
-
指定一个操作数的情况
js{ $max: <expression> }
-
指定一个数组操作数的情况
js{ $max: [ <expression1>, <expression2> ... ] }
使用
空值和缺失值的处理
对于部分文档(注意不是全部哦 )中字段为null
或缺失的情况,$max
运算符只处理非空且非缺失的字段。如果所有文档相关的字段为空或缺失,$max
返回的最大值是null
。
数组操作数的处理
对于$group
和$setWindowFields
阶段,如果表达式解析为数组,$max
不会去遍历数组,而是把数组当做一个整体来处理。
对于其他阶段:
- 对于单个操作数解析为数组的情况,
$max
会遍历数组返回最大值。 - 对于数组操作数,如果元素被解析为数组,
$max
也不会对数组进行遍历,而是把它当做一个整体。
举例
在$group阶段使用
sales
集合有下列文档:
json
{ "_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") }
下面的聚合操作根据item
字段对文档进行分组,并使用$max
运算符计算出每个分组的最大金额和数量:
js
db.sales.aggregate(
[
{
$group:
{
_id: "$item",
maxTotalAmount: { $max: { $multiply: [ "$price", "$quantity" ] } },
maxQuantity: { $max: "$quantity" }
}
}
]
)
操作返回下面的结果:
json
{ "_id" : "xyz", "maxTotalAmount" : 50, "maxQuantity" : 10 }
{ "_id" : "jkl", "maxTotalAmount" : 20, "maxQuantity" : 1 }
{ "_id" : "abc", "maxTotalAmount" : 100, "maxQuantity" : 10 }
在$setWindowFields阶段使用
使用下面的脚本创建cakeSales
集合:
js
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 }
] )
下面的聚合在$setWindowFields
阶段使用$max
输出每个州蛋糕的最高销量:
js
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
maximumQuantityForState: {
$max: "$quantity",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
操作返回下面的结果:
json
{ "_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 }
在本例中:
partitionBy: "$state"
根据state
对集合文档进行分组,共分为两组CA
和WA
sortBy: { orderDate: 1 }
根据orderDate
对每个分区进行从小到大的排序,订单日期最早的在最前面output
使用$max
将窗口文档中数量quantity
最大的值赋予maximumQuantityForState
字段
在$project阶段使用
students
集合有下列文档:
json
{ "_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 }
下面的聚合在$project
阶段使用$max
计算测验最高分、实验室最高分以及期末和期中考试的最高分:
js
db.students.aggregate([
{ $project: { quizMax: { $max: "$quizzes"}, labMax: { $max: "$labs" }, examMax: { $max: [ "$final", "$midterm" ] } } }
])
操作返回下面的结果:
json
{ "_id" : 1, "quizMax" : 10, "labMax" : 8, "examMax" : 80 }
{ "_id" : 2, "quizMax" : 10, "labMax" : 8, "examMax" : 95 }
{ "_id" : 3, "quizMax" : 5, "labMax" : 6, "examMax" : 78 }