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.有关更多信息,请参阅删除操作的计时。
To enable automatic removal of documents for an existing time series collection, issue the following 要自动删除现有时间序列集合的文档,请发出以下collMod
command:collMod
命令:
db.runCommand({ collMod: "weather24h", expireAfterSeconds: 604801 })
expireAfterSeconds
ParameterexpireAfterSeconds
参数To change the 要更改expireAfterSeconds
parameter value, issue the following collMod
command:expireAfterSeconds
参数值,请发出以下collMod
命令:
db.runCommand({ collMod: "weather24h", expireAfterSeconds: 604801 })
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: { ... } }, ... }, ... ] } }
To disable automatic removal, use the 要禁用自动删除,请使用collMod
command to set expireAfterSeconds
to off
:collMod
命令将expireAfterSeconds
设置为off
:
db.runCommand({ collMod: "weather24h", expireAfterSeconds: "off" })
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. 删除过期存储桶的后台任务每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秒之后的一段时间内存在。