0
0
MongoDBquery~10 mins

$group stage for aggregation 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 field 'category'.

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

Complete the code to calculate the total quantity for each product using $sum.

MongoDB
db.collection.aggregate([{ $group: { _id: "$product", totalQuantity: { $[1]: "$quantity" } } } }])
Drag options to blanks, or click blank then click option'
Asum
Bavg
Cmax
Dmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using $avg instead of $sum when summing values.
Not prefixing the operator with a dollar sign.
3fill in blank
hard

Fix the error in the $group stage to count documents per user.

MongoDB
db.collection.aggregate([{ $group: { _id: "$user", count: { $[1]: 1 } } } }])
Drag options to blanks, or click blank then click option'
Aavg
Btotal
Ccount
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using $count inside $group which is invalid.
Using $avg or other operators instead of $sum.
4fill in blank
hard

Fill both blanks to group by 'department' and calculate the maximum salary.

MongoDB
db.collection.aggregate([{ $group: { _id: "$"[1], maxSalary: { $[2]: "$salary" } } } }])
Drag options to blanks, or click blank then click option'
Adepartment
Bsum
Cmax
Demployee
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum instead of $max when looking for maximum.
Grouping by a wrong field name.
5fill in blank
hard

Fill all three blanks to group by 'team', calculate average age, and count members.

MongoDB
db.collection.aggregate([{ $group: { _id: "$"[1], averageAge: { $[2]: "$age" }, memberCount: { $[3]: 1 } } } }])
Drag options to blanks, or click blank then click option'
Ateam
Bavg
Csum
Ddepartment
Attempts:
3 left
💡 Hint
Common Mistakes
Using $sum instead of $avg for average calculation.
Using wrong field names for grouping.