Challenge - 5 Problems
Bucket Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Using $bucket to group ages into fixed ranges
Given a collection
people with documents containing an age field, what is the output of this aggregation pipeline?db.people.aggregate([
{
$bucket: {
groupBy: "$age",
boundaries: [0, 20, 40, 60],
default: "Other",
output: { count: { $sum: 1 } }
}
}
])MongoDB
db.people.aggregate([
{
$bucket: {
groupBy: "$age",
boundaries: [0, 20, 40, 60],
default: "Other",
output: { count: { $sum: 1 } }
}
}
])Attempts:
2 left
💡 Hint
Remember that $bucket groups values into the specified boundaries and uses the default bucket for values outside.
✗ Incorrect
The $bucket stage groups ages into ranges [0-20), [20-40), [40-60). Values outside these ranges go to the 'Other' bucket. The output counts how many documents fall into each bucket.
❓ query_result
intermediate2:00remaining
Using $bucketAuto to create automatic buckets
Consider a collection
sales with a price field. What does this aggregation output?db.sales.aggregate([
{
$bucketAuto: {
groupBy: "$price",
buckets: 3,
output: { count: { $sum: 1 } }
}
}
])MongoDB
db.sales.aggregate([
{
$bucketAuto: {
groupBy: "$price",
buckets: 3,
output: { count: { $sum: 1 } }
}
}
])Attempts:
2 left
💡 Hint
The $bucketAuto stage divides data into roughly equal groups based on the data distribution.
✗ Incorrect
$bucketAuto automatically calculates boundaries to create buckets with roughly equal counts. The output shows the ranges and counts per bucket.
📝 Syntax
advanced2:00remaining
Identify the syntax error in $bucket usage
Which option contains a syntax error in the $bucket stage?
MongoDB
db.collection.aggregate([
{
$bucket: {
groupBy: "$score",
boundaries: [0, 50, 100],
output: { count: { $sum: 1 } }
}
}
])Attempts:
2 left
💡 Hint
Check if the boundaries array has enough values for buckets.
✗ Incorrect
The boundaries array must have at least two values and define N+1 boundaries for N buckets. Option B has only two boundaries, which is invalid for creating buckets.
❓ optimization
advanced2:00remaining
Optimizing $bucketAuto for large datasets
Which option best improves performance when using $bucketAuto on a large collection with millions of documents?
Attempts:
2 left
💡 Hint
Indexes help queries and aggregations filter and group data faster.
✗ Incorrect
Adding an index on the field used in groupBy helps MongoDB quickly access and sort data, improving $bucketAuto performance on large datasets.
🧠 Conceptual
expert2:00remaining
Difference between $bucket and $bucketAuto
Which statement correctly describes a key difference between $bucket and $bucketAuto in MongoDB aggregation?
Attempts:
2 left
💡 Hint
Think about how boundaries are set in each stage.
✗ Incorrect
$bucket needs you to specify exact boundaries for buckets. $bucketAuto figures out boundaries itself to balance bucket sizes.