Complete the code to add a new field 'totalSales' that sums the 'amount' field in each document.
db.sales.aggregate([{ $addFields: { totalSales: { $[1]: "$amount" } } }])The $sum operator calculates the total sum of the specified field values.
Complete the code to group documents by 'category' and calculate the total sales per category.
db.sales.aggregate([{ $group: { _id: "$category", total: { $[1]: "$amount" } } }])The $group stage groups documents by the specified field, and $sum calculates the total amount per group.
Fix the error in the aggregation pipeline to correctly compute the average 'score' per 'player'.
db.games.aggregate([{ $group: { _id: "$player", avgScore: { $[1]: "$score" } } }])The $avg operator calculates the average value of the specified field.
Fill both blanks to create a pre-aggregation that groups by 'region' and counts the number of sales.
db.sales.aggregate([{ $group: { _id: "$ [1] ", count: { $[2]: 1 } } }])Grouping by region groups sales by that field. Using $sum: 1 counts each document.
Fill all three blanks to create a pre-aggregation pipeline that groups by 'product', calculates total 'quantity', and filters groups with total quantity greater than 100.
db.sales.aggregate([ { $group: { _id: "$ [1] ", totalQty: { $[2]: "$quantity" } } }, { $match: { totalQty: { $[3]: 100 } } } ])Group by product, sum quantity with $sum, then filter with $gt (greater than) 100.