Docs Menu
Docs Home
/
MongoDB Manual
/ / /

Expire Data from Collections by Setting TTL

On this page

  • Expire Documents in the MongoDB Atlas UI
  • Expire Documents with Filter Conditions

This document provides an introduction to MongoDB's "time to live" or TTL collection feature. 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.

You can expire data for deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

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 mongod that reads the date-typed values in the index and removes expired documents from the collection.

To create a TTL index, use createIndex(). Specify an index field that is either a date type or an array that contains date type values. Use the expireAfterSeconds option to specify a TTL value in seconds.

Note

The 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 expireAfterSeconds of an existing TTL index using the collMod command.

To expire data in the Atlas UI, follow these steps:

1
  1. In the MongoDB Atlas UI, click Database in the sidebar.

  2. For the database deployment that contains the data you want to expire, click Browse Collections.

  3. In the left navigation pane, select the database.

  4. In the left navigation pane, select the collection.

2
  1. Click the Indexes tab.

  2. Click Create Index.

3
  1. In the Fields section, enter the index key specification document. For this example, enter the following text to create an index on the expiresAfter field:

    { "expiresAfter": 1 }
  2. In the Options section, enter the expireAfterSeconds option. For this example, enter the following text to expire the data 1 second after the expiresAfter field's value:

    { expireAfterSeconds: 1 }
  3. Click Review.

  4. Click Confirm.

4
  1. In the left navigation pane, select the collection that contains the index.

  2. Click the Find tab.

  3. Click Insert Document.

  4. Click the text field under the _id field and enter the field name expiresAfter.

  5. Click the text field next to expiresAfter and enter the following value:

    2023-10-01T12:00:00.000+00:00

    This value expires data after 12:00 on October 1, 2023.

  6. Click the data type drop-down menu and change the data type value to Date.

  7. Click Insert.

    The document will expire automatically one second after the expiredAfter field's value.

    The TTL index may take 1-2 seconds to expire the document. You may need to refresh the UI to see that MongoDB Atlas deletes the expired document.

You can expire data after a specified number of seconds in the terminal. 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. A document will expire when the number of seconds in the expireAfterSeconds field has passed since the time specified in its indexed field. [1]

For example, the following operation creates an index on the log_events collection's createdAt field and specifies the expireAfterSeconds value of 3600 to set the expiration time to be one hour after the time specified by createdAt.

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

When adding documents to the log_events collection, set the createdAt field to the current time:

db.log_events.insert( {
"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.

[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.

To expire documents with specific filter expressions, you can create an index that is both a partial and a TTL index.

Create a partial TTL index:

db.foo.createIndex(
{ F: 1 },
{
name: "Partial-TTL-Index",
partialFilterExpression: { D : 1 },
expireAfterSeconds: 10
}
)

Insert two documents, one of which matches the filter expression { D : 1 } of the partialFilterExpression:

db.foo.insertMany( [
{ "F" : ISODate("2019-03-07T20:59:18.428Z"), "D" : 3},
{ "F" : ISODate("2019-03-07T20:59:18.428Z"), "D" : 1}
] )

Wait for ten seconds then query the foo collection:

db.foo.find({}, {_id: 0, F: 1, D: 1})

The document that matches the partialFilterExpression of { D : 1 } is deleted (expired). As a result, only one document remains in the foo collection:

{ "F" : ISODate("2019-03-07T20:59:18.428Z"), "D" : 3}

You can expire data at a specified clock time in the terminal. 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. 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 log_events collection's expireAt field and specifies the expireAfterSeconds value of 0:

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. For instance, the following insert() operation adds a document that should expire at July 22, 2013 14:00:00.

db.log_events.insert( {
"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. As such, the data expires at the specified expireAt value.

Back

TTL Indexes