Complete the code to group documents by the field 'category'.
db.collection.aggregate([{ $group: { _id: "$"[1] } } }])The $group stage groups documents by the specified field. Here, grouping by 'category' means documents with the same category are combined.
Complete the code to calculate the total quantity for each product using $sum.
db.collection.aggregate([{ $group: { _id: "$product", totalQuantity: { $[1]: "$quantity" } } } }])The $sum operator adds up the values of the specified field for each group.
Fix the error in the $group stage to count documents per user.
db.collection.aggregate([{ $group: { _id: "$user", count: { $[1]: 1 } } } }])To count documents, use $sum: 1 which adds 1 for each document in the group.
Fill both blanks to group by 'department' and calculate the maximum salary.
db.collection.aggregate([{ $group: { _id: "$"[1], maxSalary: { $[2]: "$salary" } } } }])Grouping by 'department' groups employees by their department. Using $max finds the highest salary in each group.
Fill all three blanks to group by 'team', calculate average age, and count members.
db.collection.aggregate([{ $group: { _id: "$"[1], averageAge: { $[2]: "$age" }, memberCount: { $[3]: 1 } } } }])Grouping by 'team' groups members by their team. $avg calculates the average age. $sum: 1 counts the number of members.