Expire Data from Collections by Setting TTL通过设置TTL使集合中的数据过期

On this page本页内容

This document provides an introduction to MongoDB's "time to live" or TTL collection feature. 本文档介绍了MongoDB的“生存时间”或TTL集合特性。TTL collections make it possible to store data in MongoDB and have the mongod automatically remove data after a specified number of seconds or at a specific clock time.TTL集合可以将数据存储在MongoDB中,并让mongod在指定的秒数或特定的时钟时间后自动删除数据。

Data expiration is useful for some classes of information, including machine generated event data, logs, and session information that only need to persist for a limited period of time.数据过期对于某些类别的信息非常有用,包括机器生成的事件数据、日志和会话信息,这些信息只需要持续一段有限的时间。

A special TTL index property supports the implementation of TTL collections. 一个特殊的TTL索引属性支持TTL集合的实现。The TTL feature relies on a background thread in mongod that reads the date-typed values in the index and removes expired documents from the collection.TTL功能依赖于mongod中的后台线程,该线程读取索引中的日期类型值并从集合中删除过期文档

Procedures过程

To create a TTL index, use the db.collection.createIndex() method with the expireAfterSeconds option on a field whose value is either a date or an array that contains date values.要创建TTL索引,请在值为日期或包含日期值的数组的字段上使用带有expireAfterSeconds选项的db.collection.createIndex()方法。

Note注意

The TTL index is a single field index. Compound indexes do not support the TTL property. TTL索引是单字段索引。复合索引不支持TTL属性。For more information on TTL indexes, see TTL Indexes.有关TTL索引的更多信息,请参阅TTL索引

You can modify the expireAfterSeconds of an existing TTL index using the collMod command.您可以使用collMod命令修改现有TTL索引的expireAfterSeconds

Expire Documents after a Specified Number of Seconds在指定的秒数后使文档过期

To expire data after a specified number of seconds has passed since the indexed field, create a TTL index on a field that holds values of BSON date type or an array of BSON date-typed objects and specify a positive non-zero value in the expireAfterSeconds field. 要在索引字段之后经过指定的秒数后使数据过期,请在保存BSON date类型值或BSON data类型对象数组的字段上创建TTL索引,并在expireAfterSeconds字段中指定一个非零值。A document will expire when the number of seconds in the expireAfterSeconds field has passed since the time specified in its indexed field. expireAfterSeconds字段中的秒数从其索引字段中指定的时间起已过时,文档将过期。[1]

For example, the following operation creates an index on the log_events collection's createdAt field and specifies the expireAfterSeconds value of 10 to set the expiration time to be ten seconds after the time specified by createdAt.例如,以下操作在log_events集合的createdAt字段上创建索引,并指定expireAfterSeconds10,以将过期时间设置为createdAt指定时间后的10秒。

db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 10 } )

When adding documents to the log_events collection, set the createdAt field to the current time:将文档添加到log_events集合时,将createdAt字段设置为当前时间:

db.log_events.insertOne( {
   "createdAt": new Date(),
   "logEvent": 2,
   "logMessage": "Success!"
} )

MongoDB will automatically delete documents from the log_events collection when the document's createdAt value [1] is older than the number of seconds specified in expireAfterSeconds.当文档的createdAt[1]早于expireAfterSeconds中指定的秒数时,MongoDB将自动从log_events集合中删除文档。

[1](1, 2) If the field contains an array of BSON date-typed objects, data expires if at least one of BSON date-typed object is older than the number of seconds specified in expireAfterSeconds.如果该字段包含一个BSON日期类型对象数组,则如果至少有一个BSON日期类型对象早于expireAfterSeconds中指定的秒数,则数据将过期。

Expire Documents at a Specific Clock Time在特定时钟时间使文档过期

To expire documents at a specific clock time, begin by creating a TTL index on a field that holds values of BSON date type or an array of BSON date-typed objects and specify an expireAfterSeconds value of 0. 要在特定时钟时间使文档过期,首先在保存BSON date类型值或BSON DATA类型对象数组的字段上创建TTL索引,并指定expireAfterSeconds0For each document in the collection, set the indexed date field to a value corresponding to the time the document should expire. 对于集合中的每个文档,将索引日期字段设置为对应于文档过期时间的值。If the indexed date field contains a date in the past, MongoDB considers the document expired.如果索引日期字段包含过去的日期,MongoDB将认为该文档已过期。

For example, the following operation creates an index on the log_events collection's expireAt field and specifies the expireAfterSeconds value of 0:例如,以下操作在log_events集合的expireAt字段上创建索引,并指定expireAfterSeconds0

db.log_events.createIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } )

For each document, set the value of expireAt to correspond to the time the document should expire. 对于每个文档,将expireAt的值设置为与文档应过期的时间相对应。For example, the following insertOne() operation adds a document that expires at July 22, 2013 14:00:00.例如,下面的insertOne()操作添加了一个在July 22, 2013 14:00:00到期的文档。

db.log_events.insertOne( {
   "expireAt": new Date('July 22, 2013 14:00:00'),
   "logEvent": 2,
   "logMessage": "Success!"
} )

MongoDB will automatically delete documents from the log_events collection when the documents' expireAt value is older than the number of seconds specified in expireAfterSeconds, i.e. 0 seconds older in this case. 当文档的ExpireEat值早于expireAfterSeconds中指定的秒数时,MongoDB将自动从log_events集合中删除文档,在这种情况下为0秒。As such, the data expires at the specified expireAt value.因此,数据将在指定的expireAt值处过期。

←  TTL IndexesUnique Indexes →