$min/$max

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Update operator families

What it does

Updates only when the provided value is lower or higher, respectively.

Syntax

{$min: { field: value }} / {$max: { field: value }}

Use case

Maintain high/low-water marks.

$min/$max: practical example
db.metrics.updateOne({ _id: id }, { $max: { highScore: newScore } })
💡 Explanation Updates only when the provided value is lower or higher, respectively. When to use it Maintain high/low-water marks.
Key Takeaways
  • Updates only when the provided value is lower or higher, respectively.
  • Typical use: Maintain high/low-water marks.