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

  • Time when the data point was recorded.当记录数据点时。
  • Metadata (sometimes referred to as source), which is a label or tag that uniquely identifies a series and rarely changes.(有时称为源),这是一个标签或标记,可以唯一标识一个系列,很少更改。
  • Measurements (sometimes referred to as metrics or values), which are the data points tracked at increments in time. Generally these are key-value pairs that change over time.(有时称为度量或值),是以时间增量跟踪的数据点。通常,这些是随时间变化的键值对。

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

Example实例Measurement测量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提供了时间序列集合。

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.您可以在UI中为MongoDB Atlas中托管的部署创建时间序列集合

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缓存使用率提高

Behavior行为

Time series collections behave like typical collections. You insert and query data as usual.时间序列集合的行为类似于典型的集合。您可以像往常一样插入和查询数据。

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.为了提高查询性能,对时间序列集合的查询使用了新的复合索引。复合索引还使用优化的存储格式。

Tip

To improve query performance, you can manually add secondary indexes on measurement fields or any field in your time series collection.为了提高查询性能,可以手动在测量字段或时间序列集合中的任何字段上添加辅助索引

Important

Backward-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或更早版本。
Warning

Do not attempt to create a time series collection or view with the name system.profile. 不要尝试创建名为system.profile的时间序列集合或视图。MongoDB 6.3 and later versions return an IllegalOperation error if you attempt to do so. 如果您尝试这样做,MongoDB 6.3及更高版本会返回一个IllegalOperation错误。Earlier MongoDB versions crash.<早期的MongoDB版本崩溃。

Get Started起步

To get started with time series collections, see Create and Query a Time Series Collection.要开始使用时间序列集合,请参阅创建和查询时间序列集合