0
0
MongoDBquery~10 mins

Aggregation for reporting dashboards 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 group documents by the "category" field.

MongoDB
db.sales.aggregate([{ $group: { _id: "$"[1] } }])
Drag options to blanks, or click blank then click option'
Acategory
Bdate
Camount
Dregion
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the dollar sign before the field name.
Using a field that doesn't exist in the documents.
2fill in blank
medium

Complete the code to calculate the total sales amount per category.

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

Fix the error in the pipeline to filter sales greater than 100 before grouping.

MongoDB
db.sales.aggregate([{ $match: { amount: { $[1]: 100 } } }, { $group: { _id: "$category", totalSales: { $sum: "$amount" } } }])
Drag options to blanks, or click blank then click option'
Aeq
Blt
Cgt
Dne
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt which means less than.
Using $eq which means equal to.
4fill in blank
hard

Fill both blanks to calculate average sales per region for sales above 50.

MongoDB
db.sales.aggregate([{ $match: { amount: { $[1]: 50 } } }, { $group: { _id: "$"[2], avgSales: { $avg: "$amount" } } }])
Drag options to blanks, or click blank then click option'
Agt
Blt
Cregion
Dcategory
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt instead of $gt for filtering.
Grouping by category instead of region.
5fill in blank
hard

Fill all three blanks to create a pipeline that filters sales in 2023, groups by product, and counts sales.

MongoDB
db.sales.aggregate([{ $match: { year: [1] } }, { $group: { _id: "$"[2], count: { $[3]: 1 } } }])
Drag options to blanks, or click blank then click option'
A2023
Bproduct
Csum
Dcategory
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string '2023' instead of number 2023.
Grouping by category instead of product.
Using $avg instead of $sum for counting.