0
0
MongoDBquery~10 mins

$avg accumulator 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 calculate the average score from the scores collection.

MongoDB
db.scores.aggregate([{ $group: { _id: null, averageScore: { $[1]: "$score" } } }])
Drag options to blanks, or click blank then click option'
Aavg
Bsum
Cmax
Dmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum instead of $avg.
Using $max or $min which find maximum or minimum values.
2fill in blank
medium

Complete the code to calculate the average price for each product category.

MongoDB
db.products.aggregate([{ $group: { _id: "$category", avgPrice: { $[1]: "$price" } } }])
Drag options to blanks, or click blank then click option'
Asum
Bavg
Ccount
Dfirst
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum which adds values instead of averaging.
Using $count which counts documents, not averages.
3fill in blank
hard

Fix the error in the aggregation to correctly compute the average rating.

MongoDB
db.reviews.aggregate([{ $group: { _id: "$productId", averageRating: { $[1]: "$rating" } } }])
Drag options to blanks, or click blank then click option'
Aavg
Bsum
Cmax
Dmin
Attempts:
3 left
💡 Hint
Common Mistakes
Missing the dollar sign before the field name.
Using $sum instead of $avg.
4fill in blank
hard

Fill both blanks to calculate the average age of users grouped by city.

MongoDB
db.users.aggregate([{ $group: { _id: "$city", [1]: { $avg: [2] } } }])
Drag options to blanks, or click blank then click option'
AaverageAge
B"$age"
C"age"
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using the field name without a dollar sign.
Using a wrong output field name like 'count'.
5fill in blank
hard

Fill all three blanks to compute the average score for each team and label the output field as 'teamAverage'.

MongoDB
db.scores.aggregate([{ $group: { _id: [1], [2]: { $[3]: "$score" } } }])
Drag options to blanks, or click blank then click option'
A"$team"
BteamAverage
Cavg
D"team"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the dollar sign in the group key.
Using $sum or other accumulators instead of $avg.
Naming the output field incorrectly.