Database Manual / Time Series

Time Series Collection Limitations时间序列集合限制

Time series collections generally behave like regular collections with several limitations.时间序列集合通常表现得像常规集合,但有几个限制。

Unsupported Features不支持的功能

MongoDB does not support the following features with time series collections:MongoDB不支持时间序列集合的以下功能:

Aggregation $merge聚合$merge

You cannot use the $merge aggregation stage to add data from another collection to a time series collection. 您不能使用$merge聚合阶段将其他集合中的数据添加到时间序列集合中。Use the $out aggregation stage to write documents to a time series collection.使用$out聚合阶段将文档写入时间序列集合。

You can use $merge to move data from a time series collection to another collection.您可以使用$merge将数据从时间序列集合移动到另一个集合。

distinct Command独特的指挥

Due to the unique data structure of time series collections, MongoDB can't efficiently index them for distinct values. 由于时间序列集合的独特数据结构,MongoDB无法有效地为它们索引不同的值。Avoid using the distinct command or db.collection.distinct() helper method on time series collections. 避免在时间序列集合上使用distinct命令或db.collection.distinct()辅助方法。Instead, use a $group aggregation to group documents by distinct values.相反,使用$group聚合按不同的值对文档进行分组。

For example, to query for distinct meta.type values on documents where meta.project = 10, instead of:例如,在meta.project = 10的文档上查询不同的meta.type值,而不是:

db.foo.distinct("meta.type", {"meta.project": 10})

Use:用途:

db.foo.createIndex({"meta.project":1, "meta.type":1})
db.foo.aggregate([{$match: {"meta.project": 10}},
{$group: {_id: "$meta.type"}}])

This works as follows:其工作原理如下:

  1. Creating a compound index on meta.project and meta.type and supports the aggregation.meta.projectmeta.type上创建复合索引,并支持聚合。
  2. The $match stage filters for documents where meta.project = 10.$match阶段筛选器用于筛选meta.project = 10的文档。
  3. The $group stage uses meta.type as the group key to output one document per unique value.$group阶段使用meta.type作为组键,为每个唯一值输出一个文档。

Geospatial Queries地理空间查询

Time series collections only support the $geoNear aggregation stage for sorting geospatial data from queries against 2dsphere indexes. 时间序列集合仅支持$geoNear聚合阶段,用于对2dsphere索引查询中的地理空间数据进行排序。You can't use $near and $nearSphere operators on time series collections不能在时间序列集合上使用$near$nearSphere运算符

Document Size文档大小

The maximum size for documents within a time series collection is 4 MB.时间序列集合中文档的最大大小为4 MB。

Extended Date Range扩展日期范围

If your time series collection contains documents with timeField timestamps before 1970-01-01T00:00:00.000Z or after 2038-01-19T03:14:07.000Z, create an index on the timeField to optimize queries.如果时间序列集合包含1970-01-01T00:00:00.000Z之前或2038-01-1T03:14:07.000Z之后具有timeField时间戳的文档,请在timeField上创建索引以优化查询。

Updates更新

Update commands must meet the following requirements:更新命令必须满足以下要求:

  • You can only match on the metaField field value.您只能匹配metaField字段值。
  • You can only modify the metaField field value.您只能修改metaField字段值。
  • Your update document can only contain update operator expressions.更新文档只能包含更新运算符表达式。
  • Your update command must not limit the number of documents to be updated. Set multi: true or use the updateMany() method.更新命令不得限制要更新的文档数量。设置multi:true或使用updateMany()方法。
  • Your update command must not set upsert: true.更新命令不得设置upsert:true

To automatically delete old data, set up automatic removal (TTL).要自动删除旧数据,请设置自动删除(TTL)

Time Series Secondary Indexes时间序列二级指标

MongoDB partially supports the following indexes on time series collections:MongoDB部分支持时间序列集合上的以下索引:

MongoDB doesn't support the following index types on time series collections:MongoDB不支持时间序列集合上的以下索引类型:

If there are secondary indexes on time series collections and you need to downgrade the feature compatibility version (FCV), you must first drop any secondary indexes that are incompatible with the downgraded FCV. 如果时间序列集合上有辅助索引,并且需要降级功能兼容性版本(FCV),则必须首先删除与降级的FCV不兼容的任何辅助索引。For more information, see setFeatureCompatibilityVersion.有关更多信息,请参阅setFeatureCompatibilityVersion

Capped Collections封顶集合

You cannot create a time series collection as a capped collection.您不能将时间序列集合创建为封顶集合

Modification of Collection Type集合类型的修改

You can only set the collection type when you create a collection:您只能在创建集合时设置集合类型:

  • You cannot convert an existing collection into a time series collection.您无法将现有集合转换为时间序列集合。
  • You cannot convert a time series collection into a different collection type.您无法将时间序列集合转换为其他集合类型。

To move data from an existing collection to a time series collection, migrate data into a time series collection.要将数据从现有集合移动到时间序列集合,请将数据迁移到时间序列集中

Modification of timeField and metaField修改timeFieldmetaField

You can only set a collection's timeField and metaField parameters when you create the collection. You cannot modify these parameters later.创建集合时,只能设置集合的timeFieldmetaField参数。以后不能修改这些参数。

Granularity粒度

Bucket Size桶大小

For any configuration of granularity parameters, the maximum size of a bucket is 1000 measurements or 125KB of data, whichever is lower. MongoDB may also enforce a lower maximum size for high cardinality data with many unique values, so that the working set of buckets fits within the WiredTiger cache.对于粒度参数的任何配置,桶的最大大小为1000个测量值或125KB的数据,以较低者为准。MongoDB还可以对具有许多唯一值的高基数数据强制执行较低的最大大小,以便工作桶集适合WiredTiger缓存

Modifying Bucket Parameters修改桶参数

Once you set a collection's granularity or the custom bucketing parameters bucketMaxSpanSeconds and bucketRoundingSeconds, you can increase the time span covered by a bucket, but not decrease it. 一旦设置了集合的granularity或自定义桶参数bucketMaxSpanSecondsbucketRoundingSeconds,就可以增加桶覆盖的时间跨度,但不能减少它。Use the collMod command to modify the parameters. For example:使用collMod命令修改参数。例如:

db.runCommand({
collMod: "timeseries",
timeseries: { bucketMaxSpanSeconds: 3600, bucketRoundingSeconds: 3600 }
})

Note

bucketMaxSpanSeconds and bucketRoundingSeconds must be equal. If you modify one parameter, you must also set the other to the same value.bucketMaxSpanSecondsbucketRoundingSeconds必须相等。如果修改一个参数,还必须将另一个参数设置为相同的值。

Sharding分片

Time series collections are subject to several sharding limitations.时间序列集合受到几个分片限制。

Sharding Administration Commands分割管理命令

You cannot run sharding administration commands on sharded time series collections.您无法对分片时间序列集合运行分片管理命令。

Shard Key Fields分割键段

When sharding time series collections, you can only specify the following fields in the shard key:在对时间序列集合进行分片时,您只能在分片键中指定以下字段:

  • The metaField
  • Sub-fields of metaField的子字段
  • The timeField

You may specify combinations of these fields in the shard key. 您可以在分片键中指定这些字段的组合。No other fields, including _id, are allowed in the shard key pattern.分片键模式中不允许有其他字段,包括_id

When you specify the shard key:当您指定分片键时:

Tip

Avoid specifying only the timeField as the shard key. 避免仅将timeField指定为分片键。Since the timeField increases monotonically, it may result in all writes appearing on a single chunk within the cluster. Ideally, data is evenly distributed across chunks.由于timeField单调增加,因此可能会导致所有写入出现在集群内的单个块上。理想情况下,数据在块之间均匀分布。

To learn how to best choose a shard key, see:要了解如何最好地选择分片键,请参阅:

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用作时间序列集合中的分片键。

Resharding重新分片

Starting in MongoDB 8.0.10, you can reshard a time series collection. All shards in the time series collection must run version 8.0.10 or later to reshard.从MongoDB 8.0.10开始,您可以重新标记时间序列集合。时间序列集合中的所有分片必须运行8.0.10或更高版本才能重新分级。

For more information, see Reshard a Collection.有关更多信息,请参阅重新分片集合

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.区域分片不支持时间序列集合。平衡器总是将分片时间序列集合中的数据均匀地分布在集群中的所有分片上。

Transactions事务

You cannot write to time series collections in transactions.您不能在事务中写入时间序列集合。

Note

MongoDB supports reads from time series collections in transactions.MongoDB支持从事务中的时间序列集合进行读取。

Views视图

Time series collections are writable non-materialized views. Limitations for views apply to time series collections.时间序列集合是可写的非物化视图。视图的限制适用于时间序列集合。

Snapshot Isolation快照隔离

Read operations on time series collections with read concern "snapshot" guarantee snapshot isolation only in the absence of concurrent drop or rename operations on collections in the read operation. 具有读取关注"snapshot"的时间序列集合上的读取操作仅在读取操作中没有对集合进行并发删除或重命名操作的情况下保证快照隔离。Re-creating a time series collection on the same namespace with different granularity setting does not yield full snapshot isolation.在具有不同粒度设置的同一命名空间上重新创建时间序列集合不会产生完全的快照隔离。