0
0
MongoDBquery~10 mins

$min and $max update operators in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $min and $max update operators
Start with document
Apply $min or $max update
Compare current field value with update value
Update
Save updated document
The $min and $max operators compare the current field value with the given value and update the field only if the condition is met.
Execution Sample
MongoDB
db.collection.updateOne(
  { _id: 1 },
  { $min: { score: 50 } }
)
This updates the document with _id 1, setting 'score' to 50 only if the current score is greater than 50.
Execution Table
StepCurrent scoreUpdate operatorUpdate valueComparisonActionNew score
170$min5070 > 50Update to 5050
250$min5050 > 50 is FalseNo update50
340$max4540 < 45Update to 4545
445$max4545 < 45 is FalseNo update45
💡 Updates stop after applying $min or $max once per document; no further changes if condition fails.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
score705050454545
Key Moments - 2 Insights
Why does the score not update when the current value equals the update value?
Because $min and $max only update if the new value is strictly less ($min) or greater ($max) than the current value, as shown in steps 2 and 4 where the condition is false.
What happens if the current value is missing or null?
MongoDB treats missing or null fields as if they don't exist, so $min or $max will set the field to the update value because the comparison treats missing as greater or less accordingly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at Step 1. What is the new score after applying $min with value 50 when current score is 70?
A50
B70
C120
DNo change
💡 Hint
Check the 'Action' and 'New score' columns in Step 1 of the execution table.
At which step does the $max operator update the score from 40 to 45?
AStep 2
BStep 3
CStep 4
DNo update occurs
💡 Hint
Look at the 'Update operator' and 'Action' columns in the execution table.
If the current score was 50 and we apply $min with 50, what happens?
AScore updates to 50
BScore updates to 0
CNo update happens
DScore increases
💡 Hint
Refer to Step 2 in the execution table where the condition is false and no update occurs.
Concept Snapshot
$min and $max update operators:
- $min sets field to given value only if given value is less than current.
- $max sets field to given value only if given value is greater than current.
- If condition is false, field stays unchanged.
- Useful for conditional updates without reading first.
- Syntax: { $min: { field: value } } or { $max: { field: value } }
Full Transcript
This visual execution trace shows how MongoDB's $min and $max update operators work. Starting with a document's field value, the update compares the current value with the given value. For $min, if the current value is greater, it updates to the smaller value. For $max, if the current value is smaller, it updates to the larger value. If the values are equal or the condition is false, no update happens. This allows conditional updates directly in the database. The execution table tracks each step, showing the current value, the operator, the comparison, and the resulting action. The variable tracker shows how the field value changes after each update. Key moments clarify common confusions about equality and missing fields. The quiz tests understanding by referencing specific steps in the execution. This helps beginners see exactly how $min and $max behave in practice.