0
0
MongoDBquery~10 mins

Time-series collections in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Time-series collections
Create time-series collection
Insert data with timestamp
MongoDB stores data optimized
Query data by time range
Return matching time-series documents
End
This flow shows creating a time-series collection, inserting timestamped data, storing it efficiently, querying by time, and returning results.
Execution Sample
MongoDB
db.createCollection("temps", {
  timeseries: { timeField: "timestamp" }
});
db.temps.insertMany([
  { timestamp: ISODate("2024-06-01T10:00:00Z"), temp: 22 },
  { timestamp: ISODate("2024-06-01T11:00:00Z"), temp: 23 }
]);
db.temps.find({ timestamp: { $gte: ISODate("2024-06-01T10:30:00Z") } })
Creates a time-series collection, inserts two temperature records with timestamps, then queries for records after 10:30.
Execution Table
StepActionInput/ConditionResult/Output
1Create collectiontimeseries with timeField 'timestamp'Collection 'temps' created as time-series
2Insert document{timestamp: 2024-06-01T10:00:00Z, temp: 22}Document stored in 'temps'
3Insert document{timestamp: 2024-06-01T11:00:00Z, temp: 23}Document stored in 'temps'
4Query documentstimestamp >= 2024-06-01T10:30:00ZReturns document with timestamp 11:00
5Query documentstimestamp >= 2024-06-01T10:30:00ZDoes not return document with timestamp 10:00
6EndNo more stepsQuery complete, results returned
💡 Query ends after returning all documents matching the time condition
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
temps collectionempty1 document (10:00)2 documents (10:00, 11:00)2 documents (10:00, 11:00)2 documents (10:00, 11:00)
query resultnonenonenone1 document (11:00)1 document (11:00)
Key Moments - 2 Insights
Why does the query only return the document with timestamp 11:00 and not 10:00?
Because the query condition is timestamp >= 2024-06-01T10:30:00Z, so only documents with timestamp at or after 10:30 are returned (see execution_table rows 4 and 5).
What is special about creating a time-series collection compared to a normal collection?
A time-series collection is optimized to store and query data with a timeField, improving performance for time-based queries (see execution_table row 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the 'temps collection' after step 3?
A2 documents with timestamps 10:00 and 11:00
B1 document with timestamp 10:00
CEmpty collection
D3 documents with timestamps 10:00, 11:00, and 12:00
💡 Hint
Check variable_tracker row for 'temps collection' after Step 3
At which step does the query return documents matching the time condition?
AStep 2
BStep 4
CStep 1
DStep 3
💡 Hint
Look at execution_table rows describing query actions and results
If the query condition changed to timestamp >= 2024-06-01T09:00:00Z, what would happen?
ANo documents would be returned
BOnly the document with timestamp 11:00 would be returned
CBoth documents with timestamps 10:00 and 11:00 would be returned
DOnly the document with timestamp 10:00 would be returned
💡 Hint
Consider the timestamps of inserted documents and the new query condition
Concept Snapshot
Time-series collections store data with a timestamp field.
Create with db.createCollection and specify timeseries and timeField.
Insert documents with timestamps.
Query by time ranges efficiently.
MongoDB optimizes storage and queries for time-series data.
Full Transcript
This visual execution trace shows how to create a MongoDB time-series collection named 'temps' with a timeField called 'timestamp'. Two documents with timestamps 10:00 and 11:00 are inserted. A query is run to find documents with timestamp greater or equal to 10:30. The query returns only the document with timestamp 11:00 because the other document is before 10:30. The variable tracker shows the collection state after each step and the query result. Key moments clarify why only some documents match the query and what makes time-series collections special. The quiz tests understanding of collection state and query results at different steps.