Docs HomeMongoDB Manual

Set up Automatic Removal for Time Series Collections (TTL)设置时间序列集合的自动删除(TTL)

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参数

To 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的当前值

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控制:

granularityCovered Time Span覆盖的时间跨度
"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秒。