Docs Menu

$max(集計)

バージョン 5.0 での変更

$max

Returns the maximum value. $max compares both value and type, using the specified BSON comparison order for values of different types.

$maxは、次のステージで使用できます。

$bucket$bucketAuto$group$setWindowFieldsステージで使用する場合、 $maxは次の構文をとります。

{ $max: <expression> }

サポートされている他のステージで使用する場合、 $maxは次の 2 つの構文のいずれかをとります。

  • $maxにはオペランドとして指定された式が 1 つあります。

    { $max: <expression> }
  • $maxにはオペランドとして指定された式のリストがあります。

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

式の詳細については、「式 」を参照してください。

操作のドキュメントのうち、$max すべてではなく の ドキュメントが、フィールドのnull 値を持つか、 フィールドが欠落している場合、 演算子はフィールドの 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.

$groupおよび$setWindowFieldsステージでは、式の結果が配列となる場合、 $maxは配列をトラバースせず、配列全体として比較します。

サポートされている他のステージでは、次のようになります。

  • 単一の式をオペランドとして使用し、その式が配列に変換されると、$max は配列内を走査して配列の数値要素に対して操作を実行し、単一の値を返します。

  • 式のリストをオペランドとして使用する場合に、式のいずれかが配列に変換されると、 は配列内を 走査せず$max 、代わりに配列を数値以外の値として扱います。

以下のドキュメントを持つ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.

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

この操作は次の結果を返します。

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

コレクション 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:

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

この操作の結果、次のドキュメントが作成されます。

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

バージョン 5.0 で追加

カリフォルニア州(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:

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
maximumQuantityForState: {
$max: "$quantity",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )

この例では、次のことが行われます。

  • partitionBy: "$state" はコレクション内のドキュメントを stateパーティショニングします。CAWA 用のパーティションがあります。

  • sortBy: { orderDate: 1 } は、各パーティション内のドキュメントを orderDate を基準に昇順(1)にソートするため、最も古い orderDate が最初になります。

  • output sets the maximumQuantityForState field to the maximum quantity value using $max that is run in a ドキュメント window.

    The ウィンドウ contains documents between an unbounded lower limit and the current document in the output. This means $max returns the maximum quantity for the documents between the beginning of the partition and the current document.

In this output, the maximum quantity for CA and WA is shown in the maximumQuantityForState field:

{ "_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 }