Challenge - 5 Problems
Time-Series Bucket Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of bucket aggregation for hourly time-series data
Given a MongoDB collection
sensor_readings with documents containing timestamp and value, what is the output of the following aggregation pipeline that buckets data by hour?MongoDB
db.sensor_readings.aggregate([
{
$bucket: {
groupBy: { $hour: "$timestamp" },
boundaries: [0, 6, 12, 18, 24],
default: "Other",
output: {
count: { $sum: 1 },
avgValue: { $avg: "$value" }
}
}
}
])Attempts:
2 left
💡 Hint
Remember that the
default bucket collects documents with groupBy values outside the specified boundaries.✗ Incorrect
The
$bucket stage groups documents by the hour extracted from timestamp. Boundaries are [0,6,12,18,24], so hours 0-5 go to bucket 0, 6-11 to bucket 6, 12-17 to bucket 12, 18-23 to bucket 18. Any hour outside these (like 24) goes to the default bucket labeled "Other".🧠 Conceptual
intermediate1:30remaining
Why use the bucket pattern for time-series data?
What is the main advantage of using the bucket pattern when storing time-series data in MongoDB?
Attempts:
2 left
💡 Hint
Think about how grouping data affects storage and query speed.
✗ Incorrect
The bucket pattern groups many time-series measurements into fewer documents (buckets), which reduces overhead and improves performance for writes and queries compared to storing each measurement as a separate document.
📝 Syntax
advanced1:30remaining
Identify the syntax error in this bucket aggregation
Which option contains a syntax error in the MongoDB aggregation pipeline that buckets data by day?
MongoDB
db.measurements.aggregate([
{
$bucket: {
groupBy: { $dayOfMonth: "$timestamp" },
boundaries: [1, 11, 21, 31],
output: {
count: { $sum: 1 },
maxValue: { $max: "$value" }
}
}
}
])Attempts:
2 left
💡 Hint
Check if the boundaries cover all possible day values in a month.
✗ Incorrect
The boundaries [1,11,21,31] cover days 1-10, 11-20, and 21-30 but exclude day 31. To include day 31, the upper boundary should be 32.
❓ optimization
advanced1:30remaining
Optimizing bucket size for time-series data
When using the bucket pattern for storing time-series data, what is a key consideration to optimize performance?
Attempts:
2 left
💡 Hint
Think about MongoDB document size limits and query speed.
✗ Incorrect
Buckets should be sized to keep documents under MongoDB's 16MB limit and to allow efficient queries. Too large buckets can cause performance issues; too small buckets increase document count and overhead.
🔧 Debug
expert2:30remaining
Debugging missing data in bucketed time-series aggregation
A developer uses this aggregation to bucket time-series data by day but notices some days are missing in the output. What is the most likely cause?
MongoDB
db.timeseries.aggregate([
{
$bucket: {
groupBy: { $dayOfMonth: "$timestamp" },
boundaries: [1, 10, 20, 31],
default: "Other",
output: { count: { $sum: 1 } }
}
}
])Attempts:
2 left
💡 Hint
Check how boundaries define bucket ranges and whether all days fall into a bucket.
✗ Incorrect
The $bucket stage includes documents where groupBy value >= lower boundary and < upper boundary. Boundaries [1,10,20,31] create buckets for days 1-9, 10-19, and 20-30. Day 10 falls exactly on a boundary and is included in the next bucket, but day 31 is excluded because upper bound is exclusive. Missing days are due to boundary ranges not covering all days.