A TTL index automatically deletes documents after a duration or at a specified time. It suits short-lived data such as logs, sessions, machine-generated events, temporary files, and expired offers. section 1.3.1 section 7.4.3
Configure a TTL index by following these rules:
- Create it with createIndex() on a single date field or an array containing date values. section 7.4.3
- Set expireAfterSeconds to the retention period, from 0 through 2147483647. section 7.4.3
- Use expireAfterSeconds: 0 when each document's BSON date specifies its exact expiration time. section 7.4.3
- Change an existing TTL setting with collMod, and remember that hidden indexes still perform TTL deletions. section 7.4.3 section 7.8
db.getSiblingDB('sample_analytics').transactions.createIndex(
{ "date": 1 },
{ expireAfterSeconds: 31536000 } // Creates a TTL index on
➥the "date" field. Documents will expire 1 year after the
➥value in the "date" field.
)
Creates a one-year TTL index on the transactions collection's date field. section 7.4.3
