Docs Menu

$minicons/link.png

On this page

  • Definition
  • Behavior
  • Examples
$min

The $min updates the value of the field to a specified value if the specified value is less than the current value of the field. The $min operator can compare values of different types, using the BSON comparison order.

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

To specify a <field> in an embedded document or in an array, use dot notation.

In MongoDB 4.4 and earlier, update operators process document fields in lexicographic order. See Update Operators Behavior for details.

If the field does not exist, the $min operator sets the field to the specified value.

For comparisons between values of different types, such as a number and a null, $min uses the BSON comparison order.

Consider the following document in the collection scores:

{ _id: 1, highScore: 800, lowScore: 200 }

The lowScore for the document currently has the value 200. The following operation uses $min to compare 200 to the specified value 150 and updates the value of lowScore to 150 since 150 is less than 200:

db.scores.update( { _id: 1 }, { $min: { lowScore: 150 } } )

The scores collection now contains the following modified document:

{ _id: 1, highScore: 800, lowScore: 150 }

The next operation has no effect since the current value of the field lowScore, i.e 150, is less than 250:

db.scores.update( { _id: 1 }, { $min: { lowScore: 250 } } )

The document remains unchanged in the scores collection:

{ _id: 1, highScore: 800, lowScore: 150 }

Consider the following document in the collection tags:

{
_id: 1,
desc: "crafts",
dateEntered: ISODate("2013-10-01T05:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16Z")
}

The following operation compares the current value of the dateEntered field, i.e. ISODate("2013-10-01T05:00:00Z"), with the specified date new Date("2013-09-25") to determine whether to update the field:

db.tags.update(
{ _id: 1 },
{ $min: { dateEntered: new Date("2013-09-25") } }
)

The operation updates the dateEntered field:

{
_id: 1,
desc: "crafts",
dateEntered: ISODate("2013-09-25T00:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16Z")
}
Tip
See also:
←  $inc$max →
Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.