Introduction

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!

Understanding `$max` and `$min` Operators

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.
`$max` and `$min` Operators in Action
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal