运算符 $max 返回一组输入值的最大值。
用作字段更新运算符时,如果指定的值大于字段的当前值,则 $max 运算符会将字段的值更新为指定值。 如果该字段不存在, $max 请创建字段并将其设置为指定值。
语法
$max: <expression>
用作字段更新运算符时:
{
$max: {
<field1>: <value1>,
<field2>: <value2>,
...
}
}
参数
| 参数 | DESCRIPTION |
|---|---|
<expression> |
解析为值的任何有效表达式。 运算符 $max 计算此表达式以确定最大值。 |
用作字段更新运算符时:
| 参数 | DESCRIPTION |
|---|---|
field |
要更新的字段的名称,其最大值。 |
value |
要与当前字段值进行比较的值。 仅当此值较大时,才会更新字段。 |
例子
请考虑存储集合中的此示例文档。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
示例 1:按类别计算最高销售额
若要计算每个类别中的最高销售额,请首先运行一个查询,按销售类别对所有文档进行分组。 然后运行$max查询,以检索所有商店中每个类别内的最高销售额。
db.stores.aggregate([{
$unwind: "$sales.salesByCategory"
},
{
$group: {
_id: "$sales.salesByCategory.categoryName",
maxSales: {
$max: "$sales.salesByCategory.totalSales"
}
}
}
])
此查询返回的前五个结果为:
[
{
"_id": "Christmas Trees",
"maxSales": 49697
},
{
"_id": "Nuts",
"maxSales": 48020
},
{
"_id": "Camping Tables",
"maxSales": 48568
},
{
"_id": "Music Theory Books",
"maxSales": 46133
},
{
"_id": "Fortified Wine",
"maxSales": 49912
}
]
示例 2:使用$max$bucket
若要在销售边界的存储桶中检索最高销售额,
db.stores.aggregate([{
$bucket: {
groupBy: "$sales.totalSales",
boundaries: [0, 1000, 5000, 10000],
default: "Other",
output: {
maxSales: {
$max: "$sales.totalSales"
}
}
}
}])
此查询返回以下结果:
[
{
"_id": 1000,
"maxSales": 4996
},
{
"_id": "Other",
"maxSales": 404106
},
{
"_id": 0,
"maxSales": 995
},
{
"_id": 5000,
"maxSales": 9999
}
]
示例 3:使用$max$setWindowFields
若要在 2023 年获取笔记本电脑的最高折扣,请首先运行查询来仅展开促销事件并筛选所选类别。 然后按公司对生成的文档进行分区,并在每个生成的分区中计算最高折扣百分比。
db.stores.aggregate([{
$unwind: "$promotionEvents"
},
{
$unwind: "$promotionEvents.discounts"
},
// Filter only Laptops category and events in 2023
{
$match: {
"promotionEvents.promotionalDates.startDate.Year": 2023,
"promotionEvents.discounts.categoryName": "Laptops"
}
},
// Use $setWindowFields to calculate average discount by city
{
$setWindowFields: {
partitionBy: "$company",
output: {
maxDiscount: {
$max: "$promotionEvents.discounts.discountPercentage",
window: {
documents: ["unbounded", "unbounded"]
}
}
}
}
},
// Group by city to return one result per city
{
$group: {
_id: "$company",
maxDiscount: {
$first: "$maxDiscount"
}
}
}
])
此查询返回的前三个结果为:
[
{
"_id": "Proseware, Inc.",
"maxDiscount": 24
},
{
"_id": "Fabrikam, Inc.",
"maxDiscount": 23
},
{
"_id": "Contoso, Ltd.",
"maxDiscount": 24
}
]
示例 4:设置最大员工容量(现场更新操作员)
若要仅在当前全职员工计数较低时将全职员工更新为 10,请使用字段上的 $max 运算符执行更新。 由于当前 fullTime 值为 3,10 大于 3,因此字段将更新为 10。
db.stores.updateOne(
{ _id: "f2a8c190-28e4-4e14-9d8b-0256e53dca66" },
{
$max: {
"staff.totalStaff.fullTime": 10
}
}
)
示例 5:多个字段更新(字段更新运算符)
若要使用最大值更新多个字段,请使用具有多个字段的 $max 运算符及其要设置的相应最大值。
db.stores.updateOne(
{ _id: "f2a8c190-28e4-4e14-9d8b-0256e53dca66" },
{
$max: {
"staff.totalStaff.partTime": 1,
"sales.totalSales": 50000
}
}
)
在这种情况下:
-
partTime(2) 自 1 < 2 日以来将保留 2 (无变化) -
totalSales(31211) 自 50000 31211 年以来将更新为 50000 >
示例 6:创建新字段(字段更新运算符)
如果字段不存在, $max 请使用指定的值创建它。
db.stores.updateOne(
{ _id: "f2a8c190-28e4-4e14-9d8b-0256e53dca66" },
{
$max: {
"staff.maxStaffCapacity": 25,
"sales.peakSalesRecord": 100000
}
}
)
示例 7:更新数组元素(字段更新运算符)
使用位置运算符更新数组元素中的最大值。
db.stores.updateOne(
{
_id: "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
"sales.salesByCategory.categoryName": "Phone Mounts"
},
{
$max: {
"sales.salesByCategory.$.totalSales": 12000
}
}
)
示例 8:跟踪峰值性能(字段更新运算符)
设置仅在超过时更新的峰值性能指标。
db.stores.updateOne(
{ _id: "f2a8c190-28e4-4e14-9d8b-0256e53dca66" },
{
$max: {
"performance.peakDailySales": 5000,
"performance.maxCustomersPerDay": 150,
"performance.highestSalesMonth": 45000
}
}
)
在这些字段更新作之后,更新的文档为:
{
"_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
"name": "Fabrikam, Inc. | Car Accessory Outlet - West Adele",
"staff": {
"totalStaff": {
"fullTime": 10,
"partTime": 2
},
"maxStaffCapacity": 25
},
"sales": {
"totalSales": 50000,
"peakSalesRecord": 100000,
"salesByCategory": [
{
"categoryName": "Phone Mounts",
"totalSales": 12000
},
{
"categoryName": "Dash Cameras",
"totalSales": 22300
}
]
},
"performance": {
"peakDailySales": 5000,
"maxCustomersPerDay": 150,
"highestSalesMonth": 45000
},
"lastPromotionDate": ISODate("2024-12-31T00:00:00.000Z"),
"inventoryDeadline": ISODate("2024-06-30T00:00:00.000Z")
}
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。