$min (Keep Smaller Value)
Last updated: 2026-10-09
$min (Keep Smaller Value)
$min writes the given value only when it is smaller than the current field value, leaving the field untouched otherwise. `{ $min: { lowPrice: 50 } }`. If the field is missing it is created. Ideal for recording the lowest price or all-time low score — a keep-the-minimum update.
| Category | Update Field |
|---|---|
| MongoDB Version | 2.6+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $min: { <field>: <value> } }
⚙️ Parameters
| field | The field to compare and update. |
|---|---|
| value | The candidate value; written only if smaller. |
Returns:The update result
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $min: only decreases, never increases
// 60 -> 50 since 50 is smaller
// 30 stays since 50 is larger
db.products.updateOne(
{ _id: 1 },
{ $min: { lowPrice: 50 } }
)
Output:
// Updated to 50 only when the current value exceeds 50; otherwise untouched
📌 Notes
- Creates the field with the given value if missing
- Good for keep-the-minimum metrics
- Pair with $max to maintain bounds
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QHow do $min and $max work together?
A$min updates on smaller values, $max on larger ones — together they maintain bounds.
QWhat happens if the field is missing?
AThe field is created with the given value.
QIs $min atomic?
AYes, atomic like other update operators.