Database Manual

Time Series时间序列

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:时间序列数据通常由以下部分组成:

This table shows examples of time series data:此表显示了时间序列数据的示例:

Example示例Metrics指标Metadata元数据
Stock data股票数据Stock price股票价格Stock ticker, exchange股票行情,事务所
Weather data天气数据Temperature温度Sensor identifier, location传感器标识符、位置
Website visitors网站访问者View count浏览次数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:在这个例子中,测量包括:

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:时间序列集合是分析随时间变化的数据的最佳选择。下表说明了时间序列数据的用例:

Industry工业Examples示例
Internet of Things (IoT)物联网(IoT)
  • Sensor data (for example, smart home devices or fleet logistics)传感器数据(例如,智能家居设备或车队物流)
  • Machine learning and artificial intelligence scraping机器学习和人工智能抓取
Financial Services金融服务
  • High frequency trading高频事务
  • Financial quantitative analysis财务定量分析
  • Banking data (for example, accounting of banking transactions over time)银行数据(例如,银行事务随时间变化的会计)
  • Stock market data股市数据
Retail and E-Commerce零售和电子商务
  • Transaction, sales, and price analysis事务、销售和价格分析
  • Inventory managementinventory管理
DevOps
  • Application logging应用日志
  • Infrastructure and network monitoring基础设施和网络监控

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还会在metaFieldtimeField字段上生成一个复合索引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 timeField as a shard key in a time series collection is deprecated.从MongoDB 8.0开始,不推荐将timeField用作时间序列集合中的分片键。

Also, starting in MongoDB 8.0, if you create a time series collection with a shard key containing the timeField, a log message is added to the log file on the primary shard. 此外,从MongoDB 8.0开始,如果您使用包含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会自动在时间序列集合的metaFieldtimeField上创建复合索引

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:要开始使用时间序列集合,请参阅以下页面上的教程: