MongoDB Documentation, Release 3.0.4 Procedures To create a TTL index (page 503), use the db.collection.createIndex() method with the expireAfterSeconds option on a field whose value is either a date (page 180) or an array that contains date values (page 180). 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 (page 503). 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. A document will expire when the number of seconds in the expireAfterSeconds field has passed since the time specified in its indexed field. 81 For example, the following operation creates an index on the log_events collection’s createdAt field and spec- ifies 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: 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 . $currentDate operator 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 . 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 . 81 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 . db.log_events.insert( { "expireAt": new Date('July 22, 2013 14:00:00'), "logEvent": 2, db.log_events.insert( { "createdAt": new Date(), "logEvent": 2, "logMessage": "Success!" } ) See also: Chapter 5. Administration 212