Skip to main content

What is the purpose of TTL indexes?

Junior MongoDB
Quick Answer TTL (Time To Live) indexes automatically delete documents after a specified number of seconds. Created with expireAfterSeconds: db.sessions.createIndex({createdAt: 1}, {expireAfterSeconds: 3600}) deletes documents after 1 hour. MongoDB runs a background cleanup process every 60 seconds. Use for: sessions, cache entries, temporary data, audit logs with retention policies.

Answer

TTL indexes automatically delete documents after a specified time, useful for sessions, logs, and temporary data.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice