$max (aggregation)

Definition

$max is available in the following stages:

When used in the $group stage, $max has thefollowing syntax and returns the maximum value that resultsfrom applying an expression to each document in a group of documentsthat share the same group by key:

  1. { $max: <expression> }

When used in the other supported stages,$max returns the maximum of the specified expressionor list of expressions for each document and has one of two syntaxes:

  • $max has one specified expression as its operand:
  1. { $max: <expression> }
  • $max has a list of specified expressions as itsoperand:
  1. { $max: [ <expression1>, <expression2> ... ] }

For more information on expressions, seeExpressions.

Behavior

Null or Missing Values

If some, but not all, documents for the $maxoperation have either a null value for the field or aremissing the field, the $max operator only considers thenon-null and the non-missing values for the field.

If all documents for the $max operation have nullvalue for the field or are missing the field, the $maxoperator returns null for the maximum value.

Array Operand

In the $group stage, if the expression resolves to anarray, $max does not traverse the array and compares the arrayas a whole.

In the other supported stages:

  • With a single expression as its operand, if the expression resolvesto an array, $max traverses into the array to operate on thenumerical elements of the array to return a single value.
  • With a list of expressions as its operand, if any of the expressionsresolves to an array, $max does not traverse into thearray but instead treats the array as a non-numerical value.

Examples

Use in $group Stage

Consider a sales collection with the following documents:

  1. { "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") }
  2. { "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") }
  3. { "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") }
  4. { "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }
  5. { "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:05:00Z") }

Grouping the documents by the item field, the following operationuses the $max accumulator to compute the maximum total amount andmaximum quantity for each group of documents.

  1. db.sales.aggregate(
  2. [
  3. {
  4. $group:
  5. {
  6. _id: "$item",
  7. maxTotalAmount: { $max: { $multiply: [ "$price", "$quantity" ] } },
  8. maxQuantity: { $max: "$quantity" }
  9. }
  10. }
  11. ]
  12. )

The operation returns the following results:

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

Use in $project Stage

A collection students contains the following documents:

  1. { "_id": 1, "quizzes": [ 10, 6, 7 ], "labs": [ 5, 8 ], "final": 80, "midterm": 75 }
  2. { "_id": 2, "quizzes": [ 9, 10 ], "labs": [ 8, 8 ], "final": 95, "midterm": 80 }
  3. { "_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, themaximum lab scores, and the maximum of the final and the midterm:

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

The operation results in the following documents:

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

In the other supported stages:

  • With a single expression as its operand, if the expression resolvesto an array, $max traverses into the array to operate on thenumerical elements of the array to return a single value.
  • With a list of expressions as its operand, if any of the expressionsresolves to an array, $max does not traverse into thearray but instead treats the array as a non-numerical value.