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 priceStock ticker, exchange股票行情,事务所
Weather data天气数据TemperatureSensor identifier, location传感器标识符、位置
Website visitors网站访问者View countURL

For efficient time series data storage, MongoDB provides time series collections.为了高效地存储时间序列数据,MongoDB提供了时间序列集合。

Time Series Collections时间序列集合

New in version 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.在时间序列集合中,对写入进行组织,以便将来自同一源的数据与来自类似时间点的其他数据点一起存储。

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 normal collections. 时间序列集合的行为与正常集合类似。You can insert and query your data as you normally would.您可以像往常一样插入和查询数据。

MongoDB treats time series collections as writable non-materialized views backed by an internal collection. MongoDB将时间序列集合视为由内部集合支持的可写非物化视图When you insert data, the internal collection automatically organizes time series data into an optimized storage format.插入数据时,内部集合会自动将时间序列数据组织为优化的存储格式。

When you query time series collections, you operate on one document per measurement. 当您查询时间序列集合时,您可以对每个度量值操作一个文档。Queries on time series collections take advantage of the optimized internal storage format and return results faster.对时间序列集合的查询利用了优化的内部存储格式,可以更快地返回结果。

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.要开始使用时间序列集合,请参阅创建和查询时间序列集合