$max演算子は、入力値のセットの最大値を返します。
フィールド更新演算子として使用する場合、 $max 演算子は、指定した値がフィールドの現在の値より大きい場合に、フィールドの値を指定した値に更新します。 フィールドが存在しない場合は、 $max フィールドを作成し、指定した値に設定します。
構文
$max: <expression>
フィールド更新演算子として使用する場合:
{
$max: {
<field1>: <value1>,
<field2>: <value2>,
...
}
}
パラメーター
| パラメーター | Description |
|---|---|
<expression> |
値に解決される任意の有効な式。
$max演算子は、この式を評価して最大値を決定します。 |
フィールド更新演算子として使用する場合:
| パラメーター | Description |
|---|---|
field |
最大値で更新するフィールドの名前。 |
value |
現在のフィールド値と比較する値。 フィールドは、この値が大きい場合にのみ更新されます。 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_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"
}
}
}
])
このクエリによって返される最初の 5 つの結果は次のとおりです。
[
{
"_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 年にノート PC の最高割引を取得するには、最初にクエリを実行して、昇格イベントだけをアンワインドし、選択したカテゴリでフィルター処理します。 次に、結果のドキュメントを会社別にパーティション分割し、結果の各パーティション内で最も高い割引率を計算します。
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"
}
}
}
])
このクエリによって返される最初の 3 つの結果は次のとおりです。
[
{
"_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")
}