0
0
MongoDBquery~10 mins

$bucket and $bucketAuto for distribution 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 create buckets with $bucket stage in MongoDB aggregation.

MongoDB
db.sales.aggregate([{ $bucket: { groupBy: "$price", boundaries: [0, 100, 200, 300], default: [1] } }])
Drag options to blanks, or click blank then click option'
A0
Bnull
C"Other"
D"Default"
Attempts:
3 left
💡 Hint
Common Mistakes
Using null or 0 as default bucket label causes unexpected results.
Omitting the default field leads to errors if values fall outside boundaries.
2fill in blank
medium

Complete the code to automatically create 4 buckets with $bucketAuto stage.

MongoDB
db.sales.aggregate([{ $bucketAuto: { groupBy: "$price", buckets: [1] } }])
Drag options to blanks, or click blank then click option'
A4
B3
C5
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number different from 4 when the task asks for 4 buckets.
Using a string instead of a number.
3fill in blank
hard

Fix the error in the $bucket stage by completing the missing field.

MongoDB
db.sales.aggregate([{ $bucket: { groupBy: "$quantity", boundaries: [1, 5, 10], [1]: "Other" } }])
Drag options to blanks, or click blank then click option'
Adefault_value
BdefaultBucket
CdefaultBucketName
Ddefault
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect field names like defaultBucket or default_value.
Omitting the default field entirely.
4fill in blank
hard

Fill both blanks to create buckets with $bucket stage and label the output count field.

MongoDB
db.sales.aggregate([{ $bucket: { groupBy: "$score", boundaries: [0, 50, 100], default: "Other", output: { [1]: { $sum: 1 }, [2]: { $avg: "$score" } } } }])
Drag options to blanks, or click blank then click option'
Acount
Baverage
Ctotal
DavgScore
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic or incorrect field names that don't match aggregation meaning.
Swapping the labels between sum and average.
5fill in blank
hard

Fill all three blanks to create an automatic bucket distribution with $bucketAuto and output count and average price.

MongoDB
db.sales.aggregate([{ $bucketAuto: { groupBy: "$price", buckets: [1], output: { [2]: { $sum: 1 }, [3]: { $avg: "$price" } } } }])
Drag options to blanks, or click blank then click option'
A5
Bcount
CaveragePrice
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong number of buckets than requested.
Mixing up output field names for sum and average.