Time series data is a sequence of data points in which insights are gained by analyzing changes over time.时间序列数据是一系列数据点,通过分析随时间的变化来获得见解。
Time series data is generally composed of these components:时间序列数据通常由以下部分组成:
Time时间: Indicates when the data point was recorded.:指示记录数据点的时间。Metadata元数据: A label or tag that identifies a data series and rarely changes. Metadata is stored in a:标识数据系列且很少更改的标签或标记。元数据存储在metaField. You cannot add ametaFieldfield to a time series document after you create it. Metadata is also known assource. For more information, see metaFields.metaField中。创建时间序列文档后,您无法将metaField字段添加到该文档中。元数据也称为source。有关详细信息,请参阅metaFields。Metrics指标: Individual data points tracked at increments in time, often displayed as key-value pairs that change over time. Metrics are also known as values.:以时间增量跟踪的单个数据点,通常显示为随时间变化的键值对。度量也被称为值。Measurements测量: Documents that contain data for all metrics at a specific point in time. A measurement includes the time, metadata, and all metrics recorded at that moment.:包含特定时间点所有指标数据的文档。度量包括时间、元数据和当时记录的所有度量。
This table shows examples of time series data:此表显示了时间序列数据的示例:
| URL |
For efficient time series data storage, MongoDB provides time series collections.为了高效地存储时间序列数据,MongoDB提供了时间序列集合。
The following example shows a measurement document for weather data:以下示例显示了天气数据的测量文档:
{
"timestamp": ISODate("2025-08-19T12:00:00Z"),
"metaField": {
"sensorId": "A1234",
"location": {
"city": "New York",
"state": "NY"
}
},
"temperature": 25.4,
"humidity": 48.2,
"pressure": 1012.5,
"windSpeed": 5.2,
"windDirection": "NW"
}
In this example, the measurement contains:在这个例子中,测量包括:
A timestamp showing when the data was recorded.显示数据记录时间的时间戳。Metadata identifying the sensor and its location.标识传感器及其位置的元数据。Multiple metrics, including temperature, humidity, pressure, wind speed, and direction, collected at the given time.在给定时间集合的多个指标,包括温度、湿度、压力、风速和风向。
Time Series Collections时间序列集合
New in version 5.0.在版本5.0中新增。
Time series collections efficiently store time series data. In time series collections, writes are organized so that data from the same source is stored alongside other data points from a similar point in time.时间序列集合高效地存储时间序列数据。在时间序列集合中,对写入进行组织,以便将来自同一源的数据与来自类似时间点的其他数据点一起存储。
You can create time series collections in the UI for deployments hosted in MongoDB Atlas.您可以在MongoDB Atlas中托管的部署的UI中创建时间序列集合。
Important
Backwards-Incompatible Feature向后不兼容功能
You must drop time series collections before downgrading:在降级之前,您必须删除时间序列集合:
MongoDB 6.0 or later to MongoDB 5.0.7 or earlier.MongoDB 6.0或更高版本到MongoDB 5.0.7或更低版本。MongoDB 5.3 to MongoDB 5.0.5 or earlier.MongoDB 5.3到MongoDB 5.0.5或更早版本。
Benefits好处
Compared to normal collections, storing time series data in time series collections improves query efficiency and reduces the disk usage for time series data and secondary indexes. 与普通集合相比,将时间序列数据存储在时间序列集合中可以提高查询效率,减少时间序列数据和辅助索引的磁盘使用量。MongoDB 6.3 and later automatically creates a compound index on the time and metadata fields for new time series collections.MongoDB 6.3及更高版本会自动为新的时间序列集合的时间和元数据字段创建复合索引。
Time series collections use an underlying columnar storage format and store data in time-order. This format provides the following benefits:时间序列集合使用底层列式存储格式,并按时间顺序存储数据。这种格式具有以下优点:
Reduced complexity for working with time series data降低处理时间序列数据的复杂性Improved query efficiency提高查询效率Reduced disk usage减少磁盘使用Reduced I/O for read operations读取操作的I/O减少Increased WiredTiger cache usageWiredTiger缓存使用率增加
Example Use Cases示例用例
Time Series collections are optimal for analyzing data over time. The following table illustrates use cases for time series data:时间序列集合是分析随时间变化的数据的最佳选择。下表说明了时间序列数据的用例:
| |
| |
| |
| DevOps |
|
Time Series collections are not intended for the following types of data:时间序列集合不适用于以下类型的数据:
Unordered data无序数据Data that is not time-dependent非时间依赖的数据
Behavior行为
Time series collections generally behave like other MongoDB collections. You insert and query data as usual.时间序列集合的行为通常与其他MongoDB集合相似。您可以像往常一样插入和查询数据。
Warning
Match expressions in update commands can only specify the metaField. You can't update other fields in a time series document. For more details, see Time Series Update Limitations.更新命令中的匹配表达式只能指定metaField。您无法更新时间序列文档中的其他字段。有关更多详细信息,请参阅时间序列更新限制。
MongoDB treats time series collections as writable non-materialized views backed by an internal collection. When you insert data, the internal collection automatically organizes time series data into an optimized storage format.MongoDB将时间序列集合视为由内部集合支持的可写非物化视图。插入数据时,内部集合会自动将时间序列数据组织为优化的存储格式。
Starting in MongoDB 6.3: if you create a new time series collection, MongoDB also generates a compound index on the metaField and timeField fields. 从MongoDB 6.3开始:如果你创建一个新的时间序列集合,MongoDB还会在metaField和timeField字段上生成一个复合索引。To improve query performance, queries on time series collections use the new compound index. The compound index also uses the optimized storage format.为了提高查询性能,对时间序列集合的查询使用了新的复合索引。复合索引还使用优化的存储格式。
Warning
Starting in MongoDB 8.0, use of the 从MongoDB 8.0开始,不推荐将timeField as a shard key in a time series collection is deprecated.timeField用作时间序列集合中的分片键。
Also, starting in MongoDB 8.0, if you create a time series collection with a shard key containing the 此外,从MongoDB 8.0开始,如果您使用包含timeField, a log message is added to the log file on the primary shard. timeField的分片键创建时间序列集合,则会在主分片上的日志文件中添加一条日志消息。In addition, a log message is added every 12 hours on the primary node of the config server replica set. 此外,每12小时在配置服务器副本集的主节点上添加一条日志消息。The log messages state that using the 日志消息指出,不推荐将timeField as a shard key in a time series collection is deprecated and you must reshard your collection using the metaField.timeField用作时间序列集合中的分片键,您必须使用metaField重新标记集合。
metaFields
Time series documents can contain a metaField with metadata about each document. MongoDB uses the metaField to group sets of documents, both for internal storage optimization and query efficiency. For more information about the metaField, see metaField Considerations.时间序列文档可以包含一个metaField,其中包含有关每个文档的元数据。MongoDB使用metaField对文档集进行分组,以优化内部存储和提高查询效率。有关metaField的更多信息,请参阅metaField注意事项。
Indexes索引
MongoDB automatically creates a compound index on both the metaField and timeField of a time series collection.MongoDB会自动在时间序列集合的metaField和timeField上创建复合索引。
Zone Sharding区域分割
Zone sharding does not support time series collections. The balancer always distributes data in sharded time series collections evenly across all shards in the cluster.区域分片不支持时间序列集合。平衡器总是将分片时间序列集合中的数据均匀地分布在集群中的所有分片上。
Next Steps后续步骤
To get started with time series collections, see the tutorials on the following pages:要开始使用时间序列集合,请参阅以下页面上的教程: