Complete the code to create buckets with $bucket stage in MongoDB aggregation.
db.sales.aggregate([{ $bucket: { groupBy: "$price", boundaries: [0, 100, 200, 300], default: [1] } }])The default field specifies the bucket for values outside the boundaries. "Other" is a common label.
Complete the code to automatically create 4 buckets with $bucketAuto stage.
db.sales.aggregate([{ $bucketAuto: { groupBy: "$price", buckets: [1] } }])The buckets field sets how many buckets to create automatically. Here, 4 buckets are requested.
Fix the error in the $bucket stage by completing the missing field.
db.sales.aggregate([{ $bucket: { groupBy: "$quantity", boundaries: [1, 5, 10], [1]: "Other" } }])The correct field name for values outside boundaries is default. Other names cause errors.
Fill both blanks to create buckets with $bucket stage and label the output count field.
db.sales.aggregate([{ $bucket: { groupBy: "$score", boundaries: [0, 50, 100], default: "Other", output: { [1]: { $sum: 1 }, [2]: { $avg: "$score" } } } }])The output fields are labels for the aggregation results. "count" for sum of documents, "avgScore" for average score.
Fill all three blanks to create an automatic bucket distribution with $bucketAuto and output count and average price.
db.sales.aggregate([{ $bucketAuto: { groupBy: "$price", buckets: [1], output: { [2]: { $sum: 1 }, [3]: { $avg: "$price" } } } }])We create 4 buckets automatically, count documents per bucket, and calculate average price per bucket.