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

CategoryUpdate Field
MongoDB Version2.6+
Example Languagemongosh (Shell)

📝 Syntax

{ $min: { <field>: <value> } }

⚙️ Parameters

fieldThe field to compare and update.
valueThe 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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏