Time-series collections help store data that changes over time, like temperature or stock prices, in an easy and efficient way.
Time-series collections in MongoDB
db.createCollection("collectionName", { timeseries: { timeField: "timestamp", metaField: "metadata", granularity: "seconds" // or "minutes", "hours" } })
timeField is the field that stores the time of each record.
metaField is optional and stores extra info about the data.
db.createCollection("temperatureReadings", { timeseries: { timeField: "time", metaField: "sensorId", granularity: "minutes" } })
db.createCollection("stockPrices", { timeseries: { timeField: "tradeTime", granularity: "seconds" } })
This example creates a time-series collection for machine metrics, inserts three records, and then queries data for machine M1 sorted by time.
db.createCollection("machineMetrics", { timeseries: { timeField: "recordedAt", metaField: "machineId", granularity: "seconds" } }) // Insert sample data db.machineMetrics.insertMany([ { recordedAt: new Date("2024-06-01T10:00:00Z"), machineId: "M1", temperature: 75, rpm: 1500 }, { recordedAt: new Date("2024-06-01T10:00:05Z"), machineId: "M1", temperature: 76, rpm: 1520 }, { recordedAt: new Date("2024-06-01T10:00:10Z"), machineId: "M2", temperature: 70, rpm: 1400 } ]) // Query data db.machineMetrics.find({ machineId: "M1" }).sort({ recordedAt: 1 })
Time-series collections optimize storage and queries for data with timestamps.
Choosing the right granularity helps MongoDB manage data efficiently.
Use metaField to store information that stays the same for many records, like sensor or machine ID.
Time-series collections store data that changes over time efficiently.
They require a time field and optionally a metadata field.
They help with fast queries and better storage for time-based data.
Practice
Solution
Step 1: Understand time-series data
Time-series data is data recorded over time, like temperature or stock prices.Step 2: Identify MongoDB time-series collection use
MongoDB time-series collections optimize storage and queries for such time-based data.Final Answer:
To efficiently store and query data that changes over time -> Option CQuick Check:
Time-series collections = efficient time-based data storage [OK]
- Confusing time-series collections with file storage
- Thinking time-series collections manage users
- Assuming they handle backups automatically
Solution
Step 1: Recall time-series collection requirements
MongoDB requires a time field to track when each data point was recorded.Step 2: Check other options
Unique _id index exists by default but is not specific to time-series; user roles and binary data fields are unrelated.Final Answer:
A field that stores the timestamp of each record -> Option DQuick Check:
Time field required = A field that stores the timestamp of each record [OK]
- Choosing unrelated fields like user roles
- Confusing default indexes with required time field
- Thinking binary data field is required
db.createCollection("sensorData", {
timeseries: {
timeField: "timestamp",
metaField: "deviceId"
}
})Solution
Step 1: Analyze the createCollection command
The command uses the timeseries option with timeField and metaField specified.Step 2: Understand MongoDB behavior
This creates a time-series collection named sensorData with timestamp as time and deviceId as metadata.Final Answer:
Creates a time-series collection with timestamp and deviceId fields -> Option AQuick Check:
timeseries option creates time-series collection [OK]
- Thinking it creates a regular or capped collection
- Assuming syntax error due to missing options
- Confusing metaField with required timeField
db.createCollection("temps", {
timeseries: {
metaField: "location"
}
})Solution
Step 1: Check timeseries options in the command
The command specifies metaField but omits the required timeField.Step 2: Recall MongoDB requirements
timeField is mandatory for time-series collections; missing it causes an error.Final Answer:
Missing required timeField in timeseries options -> Option AQuick Check:
timeField missing = error [OK]
- Thinking metaField is not allowed
- Assuming collection name is invalid
- Believing timeseries option is misspelled
timestamp (timeField) and sensorId (metaField). You want to query the average temperature for sensorId 'S1' over the last 24 hours. Which aggregation pipeline is correct?
db.sensorData.aggregate([
{ $match: { sensorId: 'S1', timestamp: { $gte: new Date(Date.now() - 86400000) } } },
{ $group: { _id: null, avgTemp: { $avg: "$temperature" } } }
])Solution
Step 1: Analyze the $match stage
The pipeline filters documents where sensorId is 'S1' and timestamp is within last 24 hours.Step 2: Analyze the $group stage
It groups all matched documents (_id: null) and calculates average temperature.Final Answer:
Correctly filters by sensorId and timestamp, then averages temperature -> Option BQuick Check:
Filter by sensorId and time, then average = Correctly filters by sensorId and timestamp, then averages temperature [OK]
- Forgetting to filter by sensorId
- Using $sort instead of $match for filtering
- Grouping by sensorId when only one sensor is queried
Ready to prove it?
Pass the test and this pattern moves unknown
