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. 过期阈值是timeField值加上指定的秒数。Consider the following document in the weather24h collection:考虑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". 该文档将在"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. 一旦一个存储桶中的所有文档都过期,删除过期存储桶的后台任务将在下一次运行时删除该存储桶。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. 删除过期存储桶的后台任务每60秒运行一次。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.因此,在文档到期、bucket中所有其他文档到期和后台任务运行之间的时间段内,文档可能会保留在集合中。

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秒之后的一段时间内存在。

←  Time Series Collection Notes and LimitationsSet Granularity for Time Series Data →