Set up Automatic Removal for Time Series Collections (TTL)设置时间序列集合的自动删除(TTL)
On this page本页内容
When you create a time series collection, you can set up automatic removal of documents older than a specified number of seconds by using the 创建时间序列集合时,可以使用expireAfterSeconds
parameter:expireAfterSeconds
参数设置自动删除超过指定秒数的文档:
db.createCollection(
"weather24h",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "hours"
},
expireAfterSeconds: 86400
}
)
The expiration threshold is the 过期阈值是timeField
field value plus the specified number of seconds. Consider the following document in the weather24h
collection:timeField
字段值加上指定的秒数。考虑weather24h
集合中的以下文档:
{
"metadata": {"sensorId": 5578, "type": "temperature"},
"timestamp": ISODate("2021-05-18T10:00:00.000Z"),
"temp": 12
}
The document would expire from the database at 该文件将于"2021-05-19T10:00:00.000Z"
. Once all documents in a bucket are expired, the background task that removes expired buckets removes the bucket during the next run. "2021-05-19T10:00:00.000Z"
从数据库中过期。一旦桶中的所有文档都过期,删除过期桶的后台任务将在下一次运行中删除该桶。See Timing of Delete Operations for more information.有关详细信息,请参阅删除操作的定时。
Enable Automatic Removal on a Collection对集合启用自动删除
To enable automatic removal of documents for an existing time series collection, issue the following 要启用自动删除现有时间序列集合的文档,请发出以下collMod
command:collMod
命令:
db.runCommand({
collMod: "weather24h",
expireAfterSeconds: 604801
})
Change the expireAfterSeconds
Parameter更改expireAfterSeconds
参数
expireAfterSeconds
ParameterTo change the 要更改expireAfterSeconds
parameter value, issue the following collMod
command:expireAfterSeconds
参数值,请发出以下collMod
命令:
db.runCommand({
collMod: "weather24h",
expireAfterSeconds: 604801
})
Retrieve the Current Value of expireAfterSeconds
检索expireAfterSeconds
的当前值
expireAfterSeconds
To retrieve the current value of 要检索expireAfterSeconds
, use the listCollections
command:expireAfterSeconds
的当前值,请使用listCollections
命令:
db.runCommand( { listCollections: 1 } )
The result document contains a document for the time series collection which contains the 结果文档包含时间序列集合的文档,该文档包含options.expireAfterSeconds
field.options.expireAfterSeconds
字段。
{
cursor: {
id: <number>,
ns: 'test.$cmd.listCollections',
firstBatch: [
{
name: <string>,
type: 'timeseries',
options: {
expireAfterSeconds: <number>,
timeseries: { ... }
},
...
},
...
]
}
}
Disable Automatic Removal禁用自动删除
To disable automatic removal, use the 要禁用自动删除,请使用collMod
command to set expireAfterSeconds
to off
:collMod
命令将expireAfterSeconds
设置为off
:
db.runCommand({
collMod: "weather24h",
expireAfterSeconds: "off"
})
Behavior行为
Timing of Delete Operations删除操作的时间安排
MongoDB doesn't guarantee that expired data will be deleted immediately upon expiration. MongoDB并不保证过期的数据会在过期后立即被删除。Once all documents in a bucket are expired, the background task that removes expired buckets removes the bucket during the next run. 一旦桶中的所有文档都过期,删除过期桶的后台任务将在下一次运行中删除该桶。The maximum span of time that a single bucket is allowed to cover is controlled by the 单个桶允许覆盖的最大时间跨度由时间序列集合的granularity
of the time series collection:granularity
控制:
granularity | |
---|---|
"seconds" (default) | one hour |
"minutes" | 24 hours |
"hours" | 30 days |
The background task that removes expired buckets runs every 60 seconds. Therefore, documents may remain in a collection during the period between the expiration of the document, the expiration of all other documents in the bucket and the running of the background task.删除过期桶的后台任务每60秒运行一次。因此,在文档到期、桶中所有其他文档到期和后台任务运行之间的时间段内,文档可能会保留在集合中。
Because the duration of the removal operation depends on the workload of your mongod instance, expired data may exist for some time beyond the 60 second period between runs of the background task.由于删除操作的持续时间取决于mongod
实例的工作负载,因此过期数据可能会存在一段时间,超过后台任务运行之间的60秒。