TTL index tự động xóa documents sau một thời gian dựa trên date field. Nó phù hợp cho session, token, temporary verification, logs ngắn hạn hoặc cache documents.
Ví dụ:
javascript
db.sessions.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 })TTL deletion không realtime tuyệt đối; background process chạy định kỳ.
Không dùng TTL nếu cần xóa đúng chính xác từng giây cho business critical workflow.