0
0
MongoDBquery~10 mins

$min and $max accumulators in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the minimum value of the "score" field in the collection.

MongoDB
db.scores.aggregate([{ $group: { _id: null, minScore: { [1]: "$score" } } }])
Drag options to blanks, or click blank then click option'
A$sum
B$max
C$min
D$avg
Attempts:
3 left
💡 Hint
Common Mistakes
Using $max instead of $min will return the maximum value.
Using $sum or $avg will return sums or averages, not minimum values.
2fill in blank
medium

Complete the code to find the maximum value of the "age" field in the collection.

MongoDB
db.users.aggregate([{ $group: { _id: null, maxAge: { [1]: "$age" } } }])
Drag options to blanks, or click blank then click option'
A$max
B$sum
C$avg
D$min
Attempts:
3 left
💡 Hint
Common Mistakes
Using $min will return the smallest value instead of the largest.
Using $sum or $avg will not return the maximum value.
3fill in blank
hard

Fix the error in the code to correctly find the minimum "price" in the collection.

MongoDB
db.products.aggregate([{ $group: { _id: null, minPrice: { [1]: "$price" } } }])
Drag options to blanks, or click blank then click option'
A$sum
B$max
C$avg
D$min
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the $ before the field name causes the query to fail.
Using $max instead of $min returns the maximum, not minimum.
4fill in blank
hard

Fill both blanks to find the maximum "rating" and minimum "price" in the collection.

MongoDB
db.items.aggregate([{ $group: { _id: null, maxRating: { [1]: "$rating" }, minPrice: { [2]: "$price" } } }])
Drag options to blanks, or click blank then click option'
A$max
B$min
C$sum
D$avg
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping $min and $max will give wrong results.
Using $sum or $avg will not find min or max values.
5fill in blank
hard

Fill all three blanks to find the maximum "score", minimum "time", and average "age" in the collection.

MongoDB
db.records.aggregate([{ $group: { _id: null, maxScore: { [1]: "$score" }, minTime: { [2]: "$time" }, avgAge: { [3]: "$age" } } }])
Drag options to blanks, or click blank then click option'
A$max
B$min
C$avg
D$sum
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up $min and $max accumulators.
Using $sum instead of $avg for average calculation.