TTL Indexes

On this page本页内容

Note

If you are removing documents to save on storage costs, consider Online Archive in MongoDB Atlas. 如果您正在删除文档以节省存储成本,请考虑MongoDB Atlas中的在线存档Online Archive automatically archives infrequently accessed data to fully-managed S3 buckets for cost-effective data tiering.在线归档会自动将不常访问的数据归档到完全管理的S3存储桶中,以实现经济高效的数据分层。

TTL indexes are special single-field indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time or at a specific clock time. TTL索引是一种特殊的单字段索引,MongoDB可以使用它在一定时间后或特定时钟时间自动从集合中删除文档。Data expiration is useful for certain types of information like machine generated event data, logs, and session information that only need to persist in a database for a finite amount of time.数据过期对于某些类型的信息很有用,例如机器生成的事件数据、日志和会话信息,这些信息只需要在数据库中保留有限的时间。

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索引,请使用db.collection.createIndex()方法并且在值为data或包含data值的数组的字段上使用expireAfterSeconds选项。

For example, to create a TTL index on the lastModifiedDate field of the eventlog collection, use the following operation in the mongo shell:例如,要在eventlog集合的lastModifiedDate字段上创建TTL索引,请在mongo shell中使用以下操作:

db.eventlog.createIndex( { "lastModifiedDate": 1 }, { expireAfterSeconds: 3600 } )

Behavior行为

Expiration of Data数据过期

TTL indexes expire documents after the specified number of seconds has passed since the indexed field value; i.e. the expiration threshold is the indexed field value plus the specified number of seconds.TTL索引在索引字段值之后经过指定的秒数后使文档过期;亦即,过期阈值是索引字段值加上指定的秒数。

If the field is an array, and there are multiple date values in the index, MongoDB uses lowest (i.e. earliest) date value in the array to calculate the expiration threshold.如果字段是一个数组,并且索引中有多个日期值,MongoDB将使用数组中的最低(即最早)日期值来计算过期阈值。

If the indexed field in a document is not a date or an array that holds a date value(s), the document will not expire.如果文档中的索引字段不是日期或包含日期值的数组,文档将不会过期。

If a document does not contain the indexed field, the document will not expire.如果文档不包含索引字段,则文档不会过期。

Delete Operations删除操作

A background thread in mongod reads the values in the index and removes expired documents from the collection.mongod中的后台线程读取索引中的值,并从集合中删除过期的文档

When the TTL thread is active, you will see delete operations in the output of db.currentOp() or in the data collected by the database profiler.当TTL线程处于活动状态时,您将在db.currentOp()或数据库探查器收集的数据中的输出中看到删除操作。

Timing of the Delete Operation删除操作的计时

MongoDB begins removing expired documents as soon as the index finishes building on the primary. 一旦索引在primary上构建完成,MongoDB就开始删除过期文档。For more information on the index build process, see Index Builds on Populated Collections.有关索引生成过程的更多信息,请参阅在填充的集合上生成索引

The TTL index does not guarantee that expired data will be deleted immediately upon expiration. TTL索引不能保证过期数据在过期后立即被删除。There may be a delay between the time a document expires and the time that MongoDB removes the document from the database.文档过期与MongoDB从数据库中删除文档之间可能存在延迟。

The background task that removes expired documents runs every 60 seconds. 删除过期文档的后台任务每60秒运行一次。As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task.因此,在文档到期和后台任务运行之间的时间段内,文档可能会保留在集合中。

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

Replica Sets副本集

On replica set members, the TTL background thread only deletes documents when a member is in state primary. 副本集成员上,TTL后台线程仅在成员处于primary状态时删除文档。The TTL background thread is idle when a member is in state secondary. 当成员处于次要状态时,TTL后台线程处于空闲状态。Secondary members replicate deletion operations from the primary.次要成员从主要成员复制删除操作。

Support for Queries支持查询

A TTL index supports queries in the same way non-TTL indexes do.TTL索引支持查询的方式与非TTL索引相同。

Restrictions限制