For AI agents: a documentation index is available at https://www.mongodb.com/es/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

$max (expression)

$max

Returns the maximum value from an array.

Note

Disambiguation

This page describes $max when used as an expression, which returns the maximum value from an array or a list of values.

You can also use $max in these other contexts:

{ $max: <expression> }

The expression must evaluate to an array.

$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.

With a single expression as its operand, if the expression resolves to an array, $max traverses the array to find the maximum value.

With a list of expressions as its operand, if any expression resolves to an array, $max does not traverse that array and instead compares the array as a whole.

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 the $project stage to find the highest rating for each movie across three rating sources: IMDb, Rotten Tomatoes viewer, and Rotten Tomatoes critic:

db.movies.aggregate( [
{
$match: {
title: {
$in: [
"The Godfather",
"Schindler's List",
"The Dark Knight"
]
},
"imdb.rating": { $gt: 0 },
"tomatoes.viewer.rating": { $gt: 0 },
"tomatoes.critic.rating": { $gt: 0 }
}
},
{
$project: {
_id: 0,
title: 1,
highestRating: {
$max: [
"$imdb.rating",
"$tomatoes.viewer.rating",
"$tomatoes.critic.rating"
]
}
}
},
{ $sort: { title: 1 } }
] )

The preceding pipeline:

  • Uses $match to filter for three specific movies that have valid ratings from all three sources.

  • Uses $project with $max and a list of expressions to compute the highest rating across imdb.rating, tomatoes.viewer.rating, and tomatoes.critic.rating for each movie.

  • Sorts the output alphabetically by title.