0
0
MongoDBquery~10 mins

Bucket pattern for time-series data in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Bucket pattern for time-series data
Receive time-series data
Group data by time intervals
Create bucket document for each interval
Store multiple data points inside bucket
Query buckets for time range
Extract individual data points from buckets
Data points are grouped into time buckets to store many points in one document, improving write and query efficiency.
Execution Sample
MongoDB
db.sensorData.insertOne({
  bucketStart: ISODate("2024-06-01T00:00:00Z"),
  readings: [
    {time: ISODate("2024-06-01T00:01:00Z"), value: 20},
    {time: ISODate("2024-06-01T00:02:00Z"), value: 21}
  ]
})
Insert a bucket document containing multiple sensor readings within a 1-hour interval.
Execution Table
StepActionData StateResult
1Receive new data point at 00:01No buckets yetPrepare to create bucket for 00:00-01:00
2Check if bucket for 00:00 existsNo bucket foundCreate new bucket document with bucketStart 00:00
3Insert data point into readings arrayBucket readings emptyReadings array now has 1 data point
4Receive new data point at 00:02Bucket for 00:00 exists with 1 readingAdd new data point to readings array
5Query data for 00:00-01:00One bucket with 2 readingsReturn bucket document with both readings
6Extract individual readings from bucketBucket readings arrayTwo separate data points available for analysis
7Receive data point at 01:05Bucket for 00:00 full, no bucket for 01:00Create new bucket for 01:00 interval
8Insert data point into new bucketNew bucket readings emptyReadings array now has 1 data point
9Query data for 01:00-02:00One bucket with 1 readingReturn bucket document with that reading
10End of demonstrationBuckets created and queriedEfficient storage and retrieval achieved
💡 No more data points to process; buckets created and queried successfully.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 7After Step 8Final
buckets{}{"00:00": [{time: "00:01", value: 20}]}{"00:00": [{time: "00:01", value: 20}, {time: "00:02", value: 21}]}{"00:00": [...], "01:00": []}{"00:00": [...], "01:00": [{time: "01:05", value: "X"}]}{"00:00": [...], "01:00": [...] }
Key Moments - 3 Insights
Why do we group multiple time-series points into one bucket document?
Grouping reduces the number of documents MongoDB must handle, improving write speed and query efficiency, as shown in steps 2-4 where multiple points go into one bucket.
How do we know which bucket a new data point belongs to?
We calculate the bucket interval from the data point's timestamp, like 00:00-01:00 for points at 00:01 and 00:02, as seen in step 1 and 2.
What happens when a data point falls outside existing buckets?
A new bucket document is created for that time interval, shown in step 7 when a point at 01:05 triggers a new bucket creation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the first bucket document created?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Check the 'Action' column for bucket creation in the execution_table.
According to variable_tracker, how many readings are in the bucket after Step 4?
A1 reading
B2 readings
C0 readings
D3 readings
💡 Hint
Look at the 'buckets' variable state after Step 4 in variable_tracker.
If a new data point at 00:30 is received, which bucket will it be added to?
ABucket starting at 00:00
BBucket starting at 01:00
CA new bucket starting at 00:30
DNo bucket, data discarded
💡 Hint
Refer to the concept_flow and execution_table steps about bucket intervals.
Concept Snapshot
Bucket pattern groups many time-series points into one document per time interval.
Each bucket document has a start time and an array of readings.
New data points go into the bucket matching their time interval.
Queries retrieve buckets for a time range, then extract points.
This reduces document count and improves performance.
Full Transcript
The bucket pattern for time-series data stores many data points in one document per time interval. When new data arrives, the system checks if a bucket for that time interval exists. If not, it creates one. Then it adds the data point to the bucket's readings array. Queries fetch buckets for a time range and extract individual points. This method improves write and query efficiency by reducing the number of documents handled. The execution table shows steps from receiving data points, creating buckets, inserting readings, to querying buckets. The variable tracker shows how the buckets variable changes as points are added. Key moments clarify why grouping helps, how buckets are chosen, and what happens when data falls outside existing buckets. The visual quiz tests understanding of bucket creation, readings count, and bucket assignment.