Trung BìnhMongoDB iconMongoDB

TTL index trong MongoDB dùng khi nào?

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.

Xem toàn bộ MongoDB cùng filter theo level & chủ đề con.

Mở danh sách MongoDB