0
0
MongoDBquery~20 mins

$min and $max update operators in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB MinMax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the document after applying $min update?
Given the document
{ "_id": 1, "score": 50 }
and the update operation
{ "$min": { "score": 40 } }
, what will be the value of score after the update?
MongoDB
db.collection.updateOne({ _id: 1 }, { $min: { score: 40 } })
db.collection.findOne({ _id: 1 })
A{ "_id": 1, "score": 40 }
B{ "_id": 1, "score": null }
C{ "_id": 1, "score": 90 }
D{ "_id": 1, "score": 50 }
Attempts:
2 left
💡 Hint
The $min operator updates the field only if the specified value is less than the current value.
query_result
intermediate
2:00remaining
What is the document after applying $max update?
Given the document
{ "_id": 2, "level": 10 }
and the update operation
{ "$max": { "level": 15 } }
, what will be the value of level after the update?
MongoDB
db.collection.updateOne({ _id: 2 }, { $max: { level: 15 } })
db.collection.findOne({ _id: 2 })
A{ "_id": 2, "level": 15 }
B{ "_id": 2, "level": 10 }
C{ "_id": 2, "level": 5 }
D{ "_id": 2, "level": null }
Attempts:
2 left
💡 Hint
The $max operator updates the field only if the specified value is greater than the current value.
📝 Syntax
advanced
2:00remaining
Which update operation is syntactically correct to use $min on nested field?
You want to update the nested field stats.score to 30 only if 30 is less than the current value. Which of the following update operations is correct?
A{ "$min": { "stats": { "score": 30 } } }
B{ "$min": { stats: { score: 30 } } }
C{ "$min": { "stats.score": 30 } }
D{ "$min": { "stats->score": 30 } }
Attempts:
2 left
💡 Hint
Use dot notation to update nested fields in MongoDB.
query_result
advanced
2:00remaining
What happens if $max is used with a value less than current field value?
Given the document
{ "_id": 3, "points": 100 }
and the update
{ "$max": { "points": 80 } }
, what will be the value of points after the update?
MongoDB
db.collection.updateOne({ _id: 3 }, { $max: { points: 80 } })
db.collection.findOne({ _id: 3 })
A{ "_id": 3, "points": 80 }
BUpdate operation fails with error
C{ "_id": 3, "points": null }
D{ "_id": 3, "points": 100 }
Attempts:
2 left
💡 Hint
The $max operator only updates if the new value is greater than the current value.
🧠 Conceptual
expert
3:00remaining
Why would you use $min and $max operators in a leaderboard system?
In a game leaderboard, you want to keep track of the highest and lowest scores a player has achieved. Which statement best explains why $min and $max operators are useful here?
A$min and $max operators automatically sort the leaderboard by scores.
B$min and $max allow atomic updates to keep track of lowest and highest scores without reading the document first.
C$min and $max operators create new fields for lowest and highest scores automatically.
D$min and $max operators delete scores that are not minimum or maximum.
Attempts:
2 left
💡 Hint
Think about how to update values safely when multiple users might update scores at the same time.