$min

Updates a field only if the specified value is less than the current field value, useful for maintaining minimum values.

Syntax

{ $min: { <field1>: <value1>, <field2>: <value2>, ... } }

Parameters

fieldstringrequired

Field name to update with minimum value

valueobjectrequired

Value to compare with current field value

Examples

Update minimum price

Update lowest price only if new price is lower

Query:

db.products.updateOne({ productId: "prod123" }, { $min: { lowestPrice: 29.99 } })

Output:

Updates lowestPrice to 29.99 only if current value is greater than 29.99

Track minimum inventory

Update minimum stock level for inventory tracking

Query:

db.inventory.updateOne({ itemId: "item456" }, { $min: { minStock: 10, lastUpdated: new Date() } })

Output:

Updates minStock only if 10 is less than current minimum stock level

Related