This document provides an introduction to MongoDB's "time to live" or collection feature. TTL collections make it possible to store data in MongoDB and have the automatically remove data after a specified number of seconds or at a specific clock time. Show 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. The TTL feature relies on a background thread in that reads the date-typed values in the index and removes expired from the collection. ProceduresTo create a , use the method with the NoteThe TTL index is a single field index. Compound indexes do not support the TTL property. For more information on TTL indexes, see TTL Indexes. You can modify the Expire Documents after a Specified Number of SecondsTo 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 For example, the following operation creates an index on the 3 collection's 4 field and specifies the expireAfterSeconds value of 6 to set the expiration time to be ten seconds after the time specified by 4.
When adding documents to the 3 collection, set the 4 field to the current time:
MongoDB will automatically delete documents from the 3 collection when the document's 4 value is older than the number of seconds specified in expireAfterSeconds .[1](, ) 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 .Expire Documents at a Specific Clock TimeTo 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 5. For 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.For example, the following operation creates an index on the 3 collection's 7 field and specifies the expireAfterSeconds value of 5:
For each document, set the value of 7 to correspond to the time the document should expire. For example, the following operation adds a document that expires at 2.
MongoDB will automatically delete documents from the 3 collection when the documents' 7 value is older than the number of seconds specified in expireAfterSeconds , i.e. 5 seconds older in this case. As such, the data expires at the specified 7 value.Indexes Configured Using NaNWarningPossible Data Loss When a TTL index has 9, upgrade, downgrade, and certain syncing operations can lead to unexpected behavior and possible data loss.Do not set 9 in your TTL index configuration.Prior to MongoDB 5.0, when a TTL index has 9, MongoDB logs an error and does not remove any records.From MongoDB 5.0.0 - 5.0.13 (and 6.0.0 - 6.0.1), 9 is treated as 5. If a TTL index is configured with expireAfterSeconds set to 9, all TTL-indexed documents expire immediately.Starting in MongoDB 5.0.14 (and 6.0.2), the server will not use TTL indexes that have 9.However, there are still some situations which may result in unexpected behavior. Documents may expire:
To avoid problems, either drop or correct any misconfigured TTL indexes. 1 Run the following script in the shell. The script does not work in the legacy 2 Use the command to update any misconfigured As an alternative, you can any misconfigured TTL indexes and recreate them later using the command. |