On this page本页内容
You must be running MongoDB 5.0.1 or later in order to change a time series collection's granularity after the collection has been created. 必须运行MongoDB 5.0.1或更高版本,才能在创建时间序列集合后更改其粒度。See MongoDB 5.0 known issues.请参阅MongoDB 5.0已知问题。
When you create a time series collection, set the granularity to the value that is the closest match to the time span between consecutive incoming measurements that have the same unique value for the 创建时间序列集合时,将粒度设置为与连续传入测量之间的时间跨度最匹配的值,这些测量具有相同的metaField
field:metaField
唯一值:
db.createCollection( "weather24h", { timeseries: { timeField: "timestamp", metaField: "metadata", granularity: "minutes" }, expireAfterSeconds: 86400 } )
Setting the 通过优化时间序列集合中数据的内部存储方式,精确设置granularity
parameter accurately improves performance by optimizing how data in the time series collection is stored internally.granularity
参数可以提高性能。
To set the parameter accurately, choose a 要准确设置参数,请选择一个granularity
value that is closest to the ingestion rate for a unique data source as specified by the value for the metaField
field.granularity
值,该值最接近metaField
值指定的唯一数据源的摄取率。
For example, if your 例如,如果metaField
data identifies weather sensors and you ingest data from each individual sensor once every 5 minutes, you should choose "minutes"
. metaField
数据识别天气传感器,并且每5分钟从每个传感器接收一次数据,则应选择"minutes"
。Even if you have thousands of sensors and the data coming in from different sensors is only seconds apart, the 即使有数千个传感器,并且来自不同传感器的数据仅相隔几秒钟,granularity
should still be based on the ingestion rate for one sensor that is uniquely identified by its metadata.granularity
仍应基于一个传感器的摄取率,该传感器由其元数据唯一标识。
In the following table, you can see the max time span of data that is stored together for each 在下表中,您可以看到为每个granularity
value:granularity
值存储在一起的数据的最大时间跨度:
granularity | |
---|---|
"seconds" (default) | one hour |
"minutes" | 24 hours |
"hours" | 30 days |
granularity
of a Time Series Collectiongranularity
To retrieve the current value of 要检索granularity
, use the listCollections
command:granularity
的当前值,请使用listCollections
命令:
db.runCommand( { listCollections: 1 } )
The result document contains a document for the time series collection which contains the 结果文档包含时间序列集合的文档,其中包含options.timeseries.granularity
field.options.timeseries.granularity
字段。
{ cursor: { id: <number>, ns: 'test.$cmd.listCollections', firstBatch: [ { name: <string>, type: 'timeseries', options: { expireAfterSeconds: <number>, timeseries: { timeField: <string>, metaField: <string>, granularity: <string>, bucketMaxSpanSeconds: <number> } }, ... }, ... ] } }
granularity
of a Time Series Collectiongranularity
To change the 要更改granularity
parameter value, issue the following collMod
command:granularity
参数值,请发出以下collMod
命令:
db.runCommand({ collMod: "weather24h", timeseries: { granularity: "hours" } })
Once the 一旦设置了granularity
is set it can only be increased by one level at a time. granularity
,一次只能增加一个级别。From 从"seconds"
to "minutes"
or from "minutes"
to "hours"
. "seconds"
到"minutes"
,或从"minutes"
到"hours"
。Other changes are not allowed. 不允许进行其他更改。If you need to change the 如果需要将粒度从granularity
from "seconds"
to "hours"
, first increase the granularity
to "minutes"
and then to "hours"
."seconds"
更改为"hours"
,请先将粒度增加到"minutes"
,然后再增加到"hours"
。
You cannot modify the 不能修改分片时间序列集合的granularity
of a sharded time series collection.granularity
。