Set Granularity for Time Series Data设置时间序列数据的粒度
On this page本页内容
When you create a time series collection, MongoDB automatically creates a 创建时间序列集合时,MongoDB会自动创建system.buckets
system collection and groups incoming time series data into buckets. system.buckets
系统集合,并将传入的时间序列数据分组到桶中。By setting granularity, you control how frequently data is bucketed based on the ingestion rate of your data.通过设置粒度,可以根据数据的摄取率控制数据的分块频率。
Starting in MongoDB 6.3, you can use the custom bucketing parameters 从MongoDB 6.3开始,您可以使用自定义的bucketMaxSpanSeconds
and bucketRoundingSeconds
to specify bucket boundaries and more accurately control how time series data is bucketed.bucketMaxSpanSeconds
和bucketRoundingSeconds
来指定桶边界,并更准确地控制时间序列数据的桶方式。
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已知问题。
Retrieve the Current Bucketing Parameters检索当前桶形参数
To retrieve current collection values, use the 要检索当前集合值,请使用listCollections
command:listCollections
命令:
db.runCommand( { listCollections: 1 } )
For time series collections, the output contains 对于时间序列集合,输出包含granularity
, bucketMaxSpanSeconds
, and bucketRoundingSeconds
parameters, if present.granularity
、bucketMaxSpanSeconds
和bucketRoundingSeconds
参数(如果存在)。
{
cursor: {
id: <number>,
ns: 'test.$cmd.listCollections',
firstBatch: [
{
name: <string>,
type: 'timeseries',
options: {
expireAfterSeconds: <number>,
timeseries: {
timeField: <string>,
metaField: <string>,
granularity: <string>,
bucketMaxSpanSeconds: <number>,
bucketRoundingSeconds: <number>
}
},
...
},
...
]
}
}
Using the "granularity" Parameter使用“粒度”参数
The following table shows the maximum time interval included in one bucket of data when using a given 下表显示了使用给定granularity
value:granularity
值时一个数据桶中包含的最大时间间隔:
granularity | granularity |
---|---|
seconds | 1 hour |
minutes | 24 hours |
hours | 30 days |
By default, 默认情况下,granularity
is set to seconds
. granularity
设置为seconds
。You can improve performance by setting the 您可以通过将granularity
value to the closest match to the time span between incoming measurements from the same data source. granularity
值设置为与来自同一数据源的传入测量值之间的时间跨度最匹配来提高性能。For example, if you are recording weather data from thousands of sensors but only record data from each sensor once per 5 minutes, set 例如,如果您正在记录数千个传感器的天气数据,但每5分钟只记录一次每个传感器的数据,请将granularity
to "minutes"
.granularity
设置为"minutes"
。
db.createCollection(
"weather24h",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "minutes"
},
expireAfterSeconds: 86400
}
)
Setting the 将granularity
to hours
groups up to a month's worth of data ingest events into a single bucket, resulting in longer traversal times and slower queries. granularity
设置为hours
将最多一个月的数据摄取事件分组到一个桶中,导致遍历时间更长,查询速度较慢。Setting it to 将其设置为seconds
leads to multiple buckets per polling interval, many of which might contain only a single document.seconds
会导致每个轮询间隔有多个桶,其中许多桶可能只包含一个文档。
See also: 另请参阅:
Using Custom Bucketing Parameters使用自定义桶形参数
In MongoDB 6.3 and higher, instead of 在MongoDB 6.3及更高版本中,您可以使用两个自定义的bucketing参数手动设置桶边界,而不是granularity
, you can set bucket boundaries manually using the two custom bucketing parameters. granularity
。Consider this approach if you need the additional precision to optimize a high volume of queries and 如果您需要额外的精度来优化大量的查询和insert
operations.insert
操作,请考虑这种方法。
To use custom bucketing parameters, set both parameters to the same value, and do not set 要使用自定义分段参数,请将两个参数设置为相同的值,并且不要设置granularity
:granularity
:
bucketMaxSpanSeconds
sets the maximum time between timestamps in the same bucket. Possible values are 1-31536000.设置同一桶中时间戳之间的最大时间。可能的值为1-31536000。bucketRoundingSeconds
sets the time interval that determines the starting timestamp for a new bucket. When a document requires a new bucket, MongoDB rounds down the document's timestamp value by this interval to set the minimum time for the bucket.设置时间间隔,用于确定新桶的开始时间戳。当文档需要一个新的桶时,MongoDB会将文档的时间戳值四舍五入这个间隔,以设置桶的最短时间。
For the weather station example with 5 minute sensor intervals, you could adjust bucketing by setting the custom bucketing parameters to 300 seconds, instead of using a 对于传感器间隔为5分钟的气象站示例,您可以通过将自定义桶形参数设置为300秒来调整桶形,而不是使用granularity
of "minutes"
:"minutes"
的granularity
:
db.createCollection(
"weather24h",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
bucketMaxSpanSeconds: 300,
bucketRoundingSeconds: 300
}
}
)
If a document with a time of 如果时间为2023-03-27T18:24:35Z
does not fit an existing bucket, MongoDB creates a new bucket with a minimum time of 2023-03-27T18:20:00Z
and a maximum time of 2023-03-27T18:24:59Z
.2023-03-27T18:24:35Z
的文档不适合现有的桶,MongoDB会创建一个新桶,最小时间为2023-03-27T18:20:00Z
,最大时间为2023-03-27T18:24:59Z
。
Change Time Series Granularity更改时间序列粒度
You can increase 您可以使用timeseries.granularity
from a shorter unit of time to a longer one using a collMod
command.collMod
命令将timeseries.granularity
从较短的时间单位增加到较长的时间单位。
db.runCommand( {
collMod: "weather24h",
timeseries: { granularity: "seconds" | "minutes" | "hours" }
} )
If you are using the custom bucketing parameters 如果您使用的是自定义bucketRoundingSeconds
and bucketMaxSpanSeconds
instead of granularity
, include both custom parameters in the collMod
command and set them to the same value:bucketRoundingSeconds
和bucketMaxSpanSeconds
参数而不是granularity
,请在collMod
命令中包括这两个自定义参数,并将它们设置为相同的值:
db.runCommand( {
collMod: "weather24h",
timeseries: {
bucketRoundingSeconds: 86400,
bucketMaxSpanSeconds: 86400
}
} )
You cannot decrease the granularity interval or the custom bucketing values.不能减少粒度间隔或自定义分段值。
To modify the granularity of a sharded time series collection, you must be running MongoDB 6.0 or later.要修改分片时间序列集合的粒度,必须运行MongoDB 6.0或更高版本。