TTL indexes support automatic expiration, but deletion is performed asynchronously. They are not exact-to-the-second authorization checks.
Before you start
You should understand documents, collections and indexes. Sketch representative documents and the reads and writes they must support. MongoDB-specific examples assume a collection with the shown fields; deployment topology, permissions and existing indexes can affect the operational behavior being discussed.
The practical goal is to reason through this situation: Expired temporary records can remain briefly while background cleanup runs. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Separate cleanup from validity
TTL deletion happens asynchronously rather than exactly at expiry.
Step 2: Check expiry on access
Authorization compares the stored deadline with the current time.
Step 3: Use TTL for retention
Background cleanup removes obsolete records after their useful lifetime.
Worked scenario
Expired temporary records can remain briefly while background cleanup runs.
A token expires at noon but remains stored shortly afterward while cleanup has not yet run. Existence must not authorize it. The application checks expiresAt before permitting access; TTL controls eventual storage reclamation, making delay harmless to the permission decision.
Common mistake
Treating record existence as proof a token is still valid is unsafe.
Verify the behavior
Test an expired record that still exists and verify authorization rejects it.
Interview exercise
Validate a time-limited token.
Answer and reasoning
Check its expiry during authorization and use TTL only for eventual storage cleanup.
Continue learning
Compare the scenario with the MongoDB interview questions and test your understanding with the MongoDB MCQs. For terminology and implementation details, consult the reference material.