For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
Docs Menu

$max (accumulator operator)

$max

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

The $max accumulator is available in these stages:

Note

Disambiguation

This page describes $max when used as an accumulator. Accumulators return a single aggregated value like sum, maximum, or minimum across a group of input documents.

You can also use $max in these other contexts:

{ $max: <expression> }

$max compares input data following the BSON Comparison order to determine the appropriate output type. When the input data contains multiple data types, the $max output type is the highest in the comparison order.

If some, but not all, input values for $max are null or missing, $max only considers the non-null and non-missing values.

If all input values for $max are null or missing, $max returns null.

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

If multiple values compare as equal, $max may return any of those values. There is no guarantee which equal value $max returns.

For example, under a case-insensitive collation, "a" and "A" can compare as equal. There is no guarantee which will be returned.

The examples on this page use data from the sample_mflix dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the Sample Dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

The following example uses $max in a $group stage to find the highest IMDb rating and longest runtime for movies in each rating category:

db.movies.aggregate( [
{
$match: {
rated: { $in: [ "G", "PG", "PG-13", "R" ] },
"imdb.rating": { $gt: 0 },
runtime: { $gt: 0 }
}
},
{ $sort: { rated: 1 } },
{
$group: {
_id: "$rated",
maxImdbRating: { $max: "$imdb.rating" },
maxRuntime: { $max: "$runtime" }
}
},
{ $sort: { _id: 1 } }
] )

The preceding pipeline:

  • Uses $match to filter for movies with a common US rating, a valid IMDb rating, and a known runtime.

  • Sorts documents by rated to ensure consistent grouping order.

  • Uses $group with $max to find the highest IMDb rating and longest runtime for each rated group.

  • Sorts the output alphabetically by rating category.

Rate this page