📖 Scenario: You are managing a MongoDB collection that stores information about products in a store. Each product has a price field. You want to update the prices so that they never go below a certain minimum price or above a certain maximum price.
🎯 Goal: Learn how to use the $min and $max update operators in MongoDB to adjust product prices to stay within specified limits.
📋 What You'll Learn
Create a MongoDB collection named products with specific product documents.
Define minimum and maximum price limits as variables.
Use the $min operator to update product prices only if the new price is lower than the current price.
Use the $max operator to update product prices only if the new price is higher than the current price.
💡 Why This Matters
🌍 Real World
Retail stores often need to adjust product prices to stay competitive or maintain profit margins. Using $min and $max operators helps automate price adjustments safely.
💼 Career
Database administrators and backend developers use MongoDB update operators like $min and $max to maintain data integrity and enforce business rules on data.
Progress0 / 4 steps
1
Create the products collection with initial data
Create a MongoDB collection called products and insert these exact documents: { _id: 1, name: "Pen", price: 10 }, { _id: 2, name: "Notebook", price: 20 }, and { _id: 3, name: "Eraser", price: 5 }.
MongoDB
Hint
Use db.products.insertMany() to add multiple documents at once.
2
Define minimum and maximum price limits
Create two variables called minPrice and maxPrice and set them to 8 and 18 respectively.
MongoDB
Hint
Use const to create variables for the price limits.
3
Use $min to lower prices only if new price is less
Write an update command using db.products.updateMany() that uses the $min operator to set the price to minPrice only if minPrice is less than the current price.
MongoDB
Hint
Use updateMany with an empty filter {} to update all documents.
4
Use $max to raise prices only if new price is more
Write an update command using db.products.updateMany() that uses the $max operator to set the price to maxPrice only if maxPrice is greater than the current price.
MongoDB
Hint
Use updateMany again with $max to raise prices where needed.
Practice
(1/5)
1. What does the $min update operator do in MongoDB?
easy
A. Updates the field only if the new value is smaller than the current value.
B. Updates the field only if the new value is larger than the current value.
C. Always updates the field with the new value.
D. Deletes the field if the new value is smaller.
Solution
Step 1: Understand the purpose of $min
The $min operator updates a field only if the new value is smaller than the current value stored in that field.
Step 2: Compare with other options
$max updates if the new value is larger, and other options do not match the behavior of $min.
Final Answer:
Updates the field only if the new value is smaller than the current value. -> Option A
Quick Check:
$min updates only if smaller [OK]
Hint: Remember: $min means update if new value is smaller [OK]
Common Mistakes:
Confusing $min with $max behavior
Thinking $min always updates the field
Assuming $min deletes fields
2. Which of the following is the correct syntax to use $max to update the field score to 90 in MongoDB?
easy
A. { $max: [ score: 90 ] }
B. { $max: score: 90 }
C. { $max: { score: 90 } }
D. { $max: { 'score' => 90 } }
Solution
Step 1: Recall correct MongoDB update syntax
MongoDB update operators use the format: { $operator: { field: value } }. So $max must be followed by an object with field and value.
Step 2: Check each option
{ $max: { score: 90 } } matches the correct syntax. The options missing braces around the field-value pair or using brackets [] have invalid syntax. { $max: { 'score' => 90 } } uses '=>' which is not valid in MongoDB queries.
Hint: Use braces { } around field-value pairs in update operators [OK]
Common Mistakes:
Using brackets [] instead of braces {}
Missing braces around field-value pairs
Using arrow syntax '=>' instead of colon ':'
3. Given a document { _id: 1, highScore: 80 }, what will be the document after applying db.collection.updateOne({_id:1}, { $max: { highScore: 75 } })?
medium
A. { _id: 1, highScore: 75 }
B. { _id: 1, highScore: 155 }
C. No document updated
D. { _id: 1, highScore: 80 }
Solution
Step 1: Understand $max behavior on existing value
The $max operator updates the field only if the new value is greater than the current value. Here, current highScore is 80, new value is 75.
Step 2: Compare values and decide update
Since 75 is less than 80, the field will NOT be updated. The document remains unchanged.
Final Answer:
{ _id: 1, highScore: 80 } -> Option D
Quick Check:
$max updates only if new value is larger [OK]
Hint: If new value < current, $max does not update [OK]
Common Mistakes:
Assuming $max always updates the field
Confusing $max with $min behavior
Thinking update fails if condition not met
4. You run this update: db.collection.updateOne({_id: 2}, { $min: { age: 30 } }) but it does not update the document. What could be the reason?
medium
A. The update syntax is incorrect.
B. The current age in the document is less than 30.
C. The document with _id: 2 does not exist.
D. The $min operator requires an array value.
Solution
Step 1: Understand $min update condition
$min updates the field only if the new value is smaller than the current value.
Step 2: Analyze why update might not happen
If the current age is already less than 30, the update will not change the document. Other options are incorrect because $min does not require arrays, the syntax is correct, and the question states the update runs but no change occurs.
Final Answer:
The current age in the document is less than 30. -> Option B
Quick Check:
$min updates only if new value smaller [OK]
Hint: No update if current value is smaller than $min value [OK]
Common Mistakes:
Thinking $min requires array values
Assuming syntax error without checking
Ignoring existing document values
5. You want to keep track of the lowest and highest temperatures recorded in a MongoDB document with fields minTemp and maxTemp. Which update operation correctly updates both fields when a new temperature temp is 15?