Hello again! Now that we've grasped the fundamentals of MongoDB update operations and arithmetic updates, it's time to delve deeper into some specific operators — $min
and $max
. Let's dive in!
The $max
and $min
operators allow us to update fields within our documents by storing either the maximum or minimum value between their current value and a new value provided. These operators are especially useful when you need to keep track of the highest or lowest record of something.
For instance, imagine you need to monitor the highest and lowest customer ratings for each comic book in a collection. Whenever a new rating is submitted, you can use $max
and $min
to potentially update the stored ratings.
Here's how they function:
$max
: Updates a specified field to a new value only if the new value is greater than the current value.$min
: Updates a specified field to a new value only if the new value is less than the current value.
