Docs 菜单
Docs 主页
/
MongoDB Manual
/

设置时间序列数据的粒度

在此页面上

  • 检索时间序列集合的 granularity
  • 更改时间序列集合的granularity

注意

您必须运行 MongoDB 5 。 0 。 1或更高版本,以便在创建时间序列集合后更改集合的粒度。请参阅 MongoDB 5 。 0已知问题。

创建时间序列集合时,请将粒度设置为与 metaField字段具有相同唯一值的连续传入测量值之间的时间跨度最接近的值:

db.createCollection(
"weather24h",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "minutes"
},
expireAfterSeconds: 86400
}
)

准确设置granularity参数可优化time-series collection中数据的内部存储方式,从而提高性能。

要准确设置参数,请选择最接近唯一数据源摄取速率的granularity值,该速率由metaField字段的值指定。

例如,如果您的metaField数据标识天气传感器,并且您每 5 分钟从每个传感器获取一次数据,则应选择"minutes" 。 即使您有数千个传感器,并且来自不同传感器的数据仅相隔几秒钟, granularity仍应基于由其元数据唯一标识的一个传感器的摄取率。

在下表中,您可以看到为每个granularity值一起存储的数据的最大时间跨度:

granularity
覆盖的时间跨度
"seconds" (默认)
一小时
"minutes"
24 小时
"hours"
30天

提示

另请参阅:

要检索 granularity 的当前值,请使用 listCollections 命令:

db.runCommand( { listCollections: 1 } )

结果文档包含时间序列集合的文档,其中包含 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 参数值,请发出以下 collMod 命令:

db.runCommand({
collMod: "weather24h",
timeseries: { granularity: "hours" }
})

设置granularity后,一次只能增加一个级别。 从"seconds""minutes"或从"minutes""hours" 。 不允许进行其他更改。 如果需要将granularity"seconds"更改为"hours" ,请先将granularity增加到"minutes" ,然后再增加到"hours"

注意

您无法修改分片时间序列集合的granularity

后退

设置时间序列集合 (TTL) 的自动删除