A time-series collection is a MongoDB collection for measurements associated with timestamps. Use one when time is central to storing and analyzing data such as sensor readings, server logs, or application performance metrics. section 2.6.3
Compared with a regular collection, it can improve query performance and reduce disk use. MongoDB groups related readings into BSON buckets by time interval and metadata, keeping data points from the same period close together. This reduces I/O when reading time-based data. Simple, flat data models make the most efficient use of the structure. section 2.6.3
To create and inspect one:
- Create a collection with db.createCollection(), setting timeseries.timeField to the timestamp field and timeseries.metaField to the field that labels a series. section 2.6.3
- List time-series collections with db.runCommand({ listCollections: 1, filter: { type: "timeseries" } }). section 2.6.3
Listing 2.4 Creating a time-series collection
db.createCollection(
"my_time_series_collection",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata"
}})
This creation command names the timestamp and metadata fields explicitly. section 2.6.3
Choose the metadata field for an identifier that rarely changes, such as the label distinguishing one measurement series from another. Once stored, timestamped data can be queried and aggregated for time-series analysis. section 2.6.3 section 1.5
