MongoDB – Minimum operator ( $min )

MongoDB provides different types of field update operators to update the values of the fields in the documents and minimum operator( $min ) is one of them. This operator updates the field with the specified value if the specified value is less than the current value.

$min operator will compare the values of different data types according to the BSON comparison order. You can also use this operator in embedded/nested documents using dot notation. One can use this operator in methods like update(), updateOne() etc. according to your requirements. If the given field does not exists, then this operator will create field and set the value of that field.

Syntax:

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

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: contributor
Document: three documents that contain the details of the contributors in the form of field-value pairs.



Comparing values (or numbers) using $min operator:

In this example, we are comparing values (or numbers) of the salary fields with the specified value, i.e, 2000. Here, the specified value is less than the current value. So. $min operator updates the value of the salary field with the help of update() method to 2000.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Mohit"}, {$min: {salary: 2000}})

chevron_right


If the current value of the salary field is greater than the specified value, then this operator will not update the value of the salary field with the specified value, i.e., 4000.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Mohit"}, {$min: {salary: 4000}})

chevron_right


Comparing values (or numbers) in nested documents using $max operator:

In this example, we are comparing values(or numbers) of the rank fields with the specified value, i.e, 13. Here, the specified value is less than the current value. So, $min operator updates the value of the salary field with the help of update() method to 13.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Priya"}, {$min: {"personal.rank": 13}})

chevron_right


If the current value of the rank field is greater than the specified value, then this operator will not update the value of the rank field with the specified value, i.e., 30.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: "Priya"}, {$min: {"personal.rank": 30}})

chevron_right





My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.