When you create a time series collection, MongoDB automatically groups incoming time series data into buckets. By setting granularity, you control how frequently data is bucketed based on the ingestion rate of your data.当您创建时间序列集合时,MongoDB会自动将传入的时间序列数据分组到桶中。通过设置粒度,您可以根据数据的摄取率控制数据被分组的频率。
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来指定桶边界,并更准确地控制时间序列数据的bucketing方式。
For more information on bucketing, see About Time Series Data.有关bucketing的更多信息,请参阅关于时间序列数据。
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或更高版本,以便在创建时间序列集合后更改其粒度。
Retrieve the Current Bucketing Parameters检索当前Bucketing参数
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>
}
},
...
},
...
]
}
}Set the "granularity" Parameter设置“粒度”参数
The following example sets the 以下示例将granularity of a weather24h collection to minutes:weather24h集合的granularity设置为minutes:
db.createCollection(
"weather24h",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "minutes"
},
expireAfterSeconds: 86400
}
)
Using Custom Bucketing Parameters使用自定义桶参数
In MongoDB 6.3 and later, instead of 在MongoDB 6.3及更高版本中,您可以使用两个自定义桶参数手动设置桶边界,而不是granularity, you can set bucket boundaries manually using the two custom bucketing parameters. Consider this approach if you expect to query data for fixed time intervals, such as every 4 hours starting at midnight. granularity。如果您希望查询固定时间间隔的数据,例如从午夜开始的每4小时一次,请考虑这种方法。Ensuring buckets don't overlap between those periods optimizes for high query volume and 确保桶在这些时段之间不重叠,可以优化高查询量和insert operations.insert操作。
To use custom bucketing parameters, set both parameters to the same value, and do not set 要使用自定义bucketing参数,请将这两个参数设置为相同的值,并且不要设置granularity:granularity:
bucketMaxSpanSecondssets the maximum time between timestamps in the same bucket. Possible values are 1-31536000.设置同一桶中时间戳之间的最大时间间隔。可能的值为1-31536000。bucketRoundingSecondssets 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, if you generate summary reports every 4 hours, you could adjust bucketing by setting the custom bucketing parameters to 14400 seconds instead of using a 以气象站为例,如果您每4小时生成一次摘要报告,则可以通过将自定义桶参数设置为14400秒而不是使用granularity of "minutes":granularity:"minutes"来调整分组:
db.createCollection(
"weather24h",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
bucketMaxSpanSeconds: 14400,
bucketRoundingSeconds: 14400
}
}
)
If a document with a time of 如果时间为2023-03-27T16:24:35Z does not fit an existing bucket, MongoDB creates a new bucket with a minimum time of 2023-03-27T16:00:00Z and a maximum time of 2023-03-27T19:59:59Z.2023-03-27T16:24:35Z的文档不适合现有的桶,MongoDB将创建一个最小时间为2023:03-27T16:00:00Z、最大时间为2023-103-27T19:59: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而不是粒度,请在collMod命令中包含这两个自定义参数并将其设置为相同的值:
db.runCommand( {
collMod: "weather24h",
timeseries: {
bucketRoundingSeconds: 86400,
bucketMaxSpanSeconds: 86400
}
} )
You cannot decrease the granularity interval or the custom bucketing values.您不能减小粒度间隔或自定义桶值。
Note
To modify the granularity of a sharded time series collection, you must be running MongoDB 6.0 or later.要修改分片时间序列集合的粒度,您必须运行MongoDB 6.0或更高版本。