0
0
MongoDBquery~20 mins

Bucket pattern for time-series data in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Time-Series Bucket Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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" }
      }
    }
  }
])
A[ { _id: 0, count: 5, avgValue: 20 }, { _id: 6, count: 3, avgValue: 15 }, { _id: 12, count: 7, avgValue: 30 }, { _id: 18, count: 2, avgValue: 25 } ]
B[ { _id: 0, count: 5, avgValue: 20 }, { _id: 6, count: 3, avgValue: 15 }, { _id: 12, count: 7, avgValue: 30 } ]
C[ { _id: 0, count: 5, avgValue: 20 }, { _id: 6, count: 3, avgValue: 15 }, { _id: 12, count: 7, avgValue: 30 }, { _id: 18, count: 2, avgValue: 25 }, { _id: "Other", count: 1, avgValue: 10 } ]
D[ { _id: 0, count: 5, avgValue: 20 }, { _id: 6, count: 3, avgValue: 15 }, { _id: 12, count: 7, avgValue: 30 }, { _id: 18, count: 2, avgValue: 25 }, { _id: 24, count: 1, avgValue: 10 } ]
Attempts:
2 left
💡 Hint
Remember that the default bucket collects documents with groupBy values outside the specified boundaries.
🧠 Conceptual
intermediate
1: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?
AIt converts time-series data into relational tables for compatibility with SQL.
BIt automatically indexes all fields in the collection for faster queries.
CIt encrypts the data to improve security for time-series measurements.
DIt reduces the number of documents by grouping multiple measurements into one document, improving write and query efficiency.
Attempts:
2 left
💡 Hint
Think about how grouping data affects storage and query speed.
📝 Syntax
advanced
1: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" }
      }
    }
  }
])
ABoundaries array must include the upper bound 32 to cover all days.
BMissing comma after the $bucket stage closing brace.
CThe groupBy expression must be a string field name, not an expression.
DThe output field names cannot be count or maxValue.
Attempts:
2 left
💡 Hint
Check if the boundaries cover all possible day values in a month.
optimization
advanced
1: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?
AAvoid using buckets and store each measurement as a separate document for maximum flexibility.
BChoose bucket sizes that balance document size limits and query efficiency by grouping a reasonable number of measurements per bucket.
CAlways use a bucket size of exactly 1000 measurements to standardize storage.
DChoose bucket sizes that are too large to minimize the number of documents regardless of document size limits.
Attempts:
2 left
💡 Hint
Think about MongoDB document size limits and query speed.
🔧 Debug
expert
2: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 } }
    }
  }
])
AThe boundaries do not cover all days; days 10 and 20 are excluded because boundaries are exclusive of the upper bound.
BThe $dayOfMonth operator returns zero-based days, so boundaries should start at 0.
CThe default bucket is incorrectly labeled "Other" and causes days to be skipped.
DThe aggregation pipeline is missing a $sort stage to order days correctly.
Attempts:
2 left
💡 Hint
Check how boundaries define bucket ranges and whether all days fall into a bucket.