Databases 7 min read

Understanding MongoDB TTL Indexes: Concepts, Operation, Creation Methods, Limitations, and Best Practices

This article explains MongoDB TTL indexes, covering their basic concept as single‑field auto‑deletion indexes, how the background process works, alternative creation methods using an expireAt field, practical limitations, and recommendations for designing efficient data expiration strategies.

360 Tech Engineering
360 Tech Engineering
360 Tech Engineering
Understanding MongoDB TTL Indexes: Concepts, Operation, Creation Methods, Limitations, and Best Practices

Many MongoDB users are familiar with TTL indexes and typically set expireAfterSeconds to let data expire, but this article examines potential drawbacks and more elegant ways to handle unwanted data.

Basic Concept : A TTL index is a single‑field index that automatically deletes documents from a collection when the indexed field’s value plus the configured expiration time is reached.

How It Works : On a primary node (replica set or sharded replica set) or a standalone instance, a background thread compares the current ISO time with the value of the indexed field plus expireAfterSeconds . If the condition is met, the thread reads the index entry and deletes the corresponding document. The thread is awakened every 60 seconds, not every expireAfterSeconds , and high server load can delay deletions.

Alternative Creation Method (using an expireAt field):

db.log_events.createIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } )

Setting expireAfterSeconds to 0 does not delete documents immediately; instead, the index uses the date stored in the expireAt field to determine when a document should be removed.

Practical Scenario : For a collection that receives millions of records daily but only needs to retain the most recent 30 days, you can either batch‑delete data manually or use a TTL index with expireAt to spread deletions over time, reducing sudden load spikes.

Example code for the batch‑delete approach:

use test13db
db.eventlog.save({lastModifiedDate:ISODate("2021-05-16T14:55:22.338Z")})
db.eventlog.createIndex({"lastModifiedDate":1}, {expireAfterSeconds:5})

Another example showing how to set expireAt for distributed expiration:

db.eventlog.save({event_name:"delete", lastModifiedDate:ISODate("2021-06-17T00:00:00.000Z")})

Limitations of TTL Indexes :

TTL indexes are single‑field indexes.

The _id field does not support TTL indexes.

TTL indexes do not work on capped collections.

You cannot modify the expiration time with createIndex ; you must use collMod :

db.runCommand({ collMod: "eventlog", index: { keyPattern: { lastModifiedDate: 1 }, expireAfterSeconds: 3600 } })

You cannot create another index on a field that already has an index.

The indexed field must be of BSON date type; timestamps or string dates are not supported.

Summary :

Design a dispersion strategy for new collections that require timed deletions.

For existing large collections, avoid immediately setting expireAfterSeconds to 0 without understanding the alternative expireAt method.

TTL indexes also serve as regular query indexes, not just for deletion.

Modifying expireAfterSeconds with collMod acquires a DB‑level write lock, so plan changes carefully.

PerformanceMongoDBData ExpirationDatabase MaintenanceTTL Index
360 Tech Engineering
Written by

360 Tech Engineering

Official tech channel of 360, building the most professional technology aggregation platform for the brand.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.