Set Granularity for Time Series Data设置时间序列数据的粒度

On this page本页内容

Note注意

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值存储在一起的数据的最大时间跨度:

granularityCovered Time Span覆盖时间跨度
"seconds" (default)one hour
"minutes"24 hours
"hours"30 days

Retrieve the granularity of a Time Series Collection检索时间序列集合的granularity

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>
               }
            },
            ...
         },
         ...
       ]
    }
 }

Change the granularity of a Time Series Collection更改时间序列集合的granularity

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"

Note注意

You cannot modify the granularity of a sharded time series collection.不能修改分片时间序列集合的granularity

←  Set up Automatic Removal for Time Series Collections (TTL)Add Secondary Indexes on metaField and timeField →