Complete the code to group documents by the "category" field.
db.sales.aggregate([{ $group: { _id: "$"[1] } }])The $group stage groups documents by the specified field. Here, grouping by category collects sales per category.
Complete the code to calculate the total sales amount per category.
db.sales.aggregate([{ $group: { _id: "$category", totalSales: { $[1]: "$amount" } } }])$avg instead of $sum for totals.The $sum operator adds up all values of the specified field, here summing amount per category.
Fix the error in the pipeline to filter sales greater than 100 before grouping.
db.sales.aggregate([{ $match: { amount: { $[1]: 100 } } }, { $group: { _id: "$category", totalSales: { $sum: "$amount" } } }])$lt which means less than.$eq which means equal to.The $gt operator filters documents where amount is greater than 100.
Fill both blanks to calculate average sales per region for sales above 50.
db.sales.aggregate([{ $match: { amount: { $[1]: 50 } } }, { $group: { _id: "$"[2], avgSales: { $avg: "$amount" } } }])$lt instead of $gt for filtering.category instead of region.First, filter sales with $gt 50, then group by region to find average sales per region.
Fill all three blanks to create a pipeline that filters sales in 2023, groups by product, and counts sales.
db.sales.aggregate([{ $match: { year: [1] } }, { $group: { _id: "$"[2], count: { $[3]: 1 } } }])category instead of product.$avg instead of $sum for counting.The pipeline filters documents where year is 2023, groups by product, and counts sales by summing 1 for each document.