Skip to main content

$max

The $max operator returns the larger of two values. If the values are of numeric type, it compares them numerically. If they are strings, it performs lexicographic comparison.

Input

  • An array containing exactly two values to compare

Output

  • The larger of the two input values

Examples

Numeric Comparison

{
  "$max": [
    5,
    10
  ]
}
Result: 10

String Comparison

{
  "$max": [
    "apple",
    "banana"
  ]
}
Result: "banana"

Field Value Comparison

{
  "$max": [
    {
      "$input": "score1"
    },
    {
      "$input": "score2"
    }
  ]
}
Result: The higher score between score1 and score2