0
0
MongoDBquery~10 mins

Why aggregation operators matter in MongoDB - Test Your Understanding

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

Complete the code to calculate the total number of documents in the collection.

MongoDB
db.sales.aggregate([{ $group: { _id: null, total: { $[1]: 1 } } }])
Drag options to blanks, or click blank then click option'
Asum
Bcount
Cavg
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using $count inside $group is invalid.
Using $avg or $max will not count documents.
2fill in blank
medium

Complete the code to find the average price of all products.

MongoDB
db.products.aggregate([{ $group: { _id: null, averagePrice: { $[1]: "$price" } } }])
Drag options to blanks, or click blank then click option'
Amax
Bsum
Cmin
Davg
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum will give total, not average.
Using $min or $max will give smallest or largest value, not average.
3fill in blank
hard

Fix the error in the aggregation to find the maximum score.

MongoDB
db.scores.aggregate([{ $group: { _id: null, maxScore: { $[1]: "$score" } } }])
Drag options to blanks, or click blank then click option'
Amax_value
BmaxScore
Cmax
Dmaximum
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'maximum' or 'maxScore' as operator names causes errors.
Misspelling the operator leads to failure.
4fill in blank
hard

Fill both blanks to calculate the total sales and average quantity.

MongoDB
db.orders.aggregate([{ $group: { _id: "$product", totalSales: { $[1]: "$amount" }, averageQty: { $[2]: "$quantity" } } }])
Drag options to blanks, or click blank then click option'
Asum
Bavg
Cmax
Dmin
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up $sum and $avg operators.
Using $max or $min instead of sum or average.
5fill in blank
hard

Fill all three blanks to group by category, count items, and find max price.

MongoDB
db.items.aggregate([{ $group: { _id: "$category", count: { $[1]: 1 }, maxPrice: { $[2]: "$price" }, minPrice: { $[3]: "$price" } } }])
Drag options to blanks, or click blank then click option'
Asum
Bmax
Cmin
Davg
Attempts:
3 left
💡 Hint
Common Mistakes
Using $avg instead of $sum for counting.
Confusing $max and $min operators.