0
0
MongoDBquery~10 mins

TTL indexes for auto-expiry in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - TTL indexes for auto-expiry
Create TTL Index on Date Field
MongoDB Monitors Documents
Check Document Date + TTL Value
Not Expired
Keep Document
Repeat Continuously
MongoDB uses a TTL index on a date field to automatically delete documents after a set time. It continuously checks documents and removes expired ones.
Execution Sample
MongoDB
db.sessions.createIndex({"lastAccess": 1}, {expireAfterSeconds: 3600})
Creates a TTL index on the 'lastAccess' field to auto-delete documents 1 hour after the timestamp.
Execution Table
StepDocument _idlastAccessCurrent TimeExpired?Action
1doc12024-06-01T10:00:00Z2024-06-01T10:30:00ZNoKeep document
2doc22024-06-01T09:00:00Z2024-06-01T10:30:00ZYesDelete document
3doc32024-06-01T10:15:00Z2024-06-01T10:30:00ZNoKeep document
4doc12024-06-01T10:00:00Z2024-06-01T11:01:00ZYesDelete document
5doc32024-06-01T10:15:00Z2024-06-01T11:16:00ZYesDelete document
6--2024-06-01T11:16:00Z-No more documents to check
💡 All documents expired or kept based on TTL; process repeats continuously.
Variable Tracker
Document _idInitial lastAccessAfter Step 1After Step 4Final
doc12024-06-01T10:00:00ZExistsDeletedDeleted
doc22024-06-01T09:00:00ZDeletedDeletedDeleted
doc32024-06-01T10:15:00ZExistsExistsDeleted
Key Moments - 2 Insights
Why does MongoDB delete documents only after the TTL time has passed, not exactly at the moment?
MongoDB checks for expired documents periodically, not continuously every second. This is why deletion happens shortly after the TTL time, as shown in steps 2 and 4 in the execution table.
Can TTL indexes be used on any field type?
No, TTL indexes only work on fields that store date values. This is why the 'lastAccess' field must be a date, as shown in the execution table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at step 2, why is 'doc2' deleted?
ABecause it was manually deleted
BBecause it has the newest lastAccess date
CBecause its lastAccess plus TTL is before the current time
DBecause TTL indexes delete all documents immediately
💡 Hint
Check the 'Expired?' column at step 2 in the execution table.
At which step does 'doc1' get deleted according to the execution table?
AStep 4
BStep 1
CStep 2
DStep 6
💡 Hint
Look for 'doc1' with 'Delete document' action in the execution table.
If the TTL was set to 1800 seconds (30 minutes) instead of 3600, how would the action for 'doc3' at step 3 change?
AIt would be deleted at step 3
BIt would still be kept at step 3
CIt would be deleted at step 1
DIt would never be deleted
💡 Hint
Compare the TTL value with the time difference between lastAccess and current time at step 3.
Concept Snapshot
TTL Indexes in MongoDB:
- Created on a date field with expireAfterSeconds option.
- MongoDB periodically checks documents.
- Documents older than (date + TTL) are deleted automatically.
- Only works on date fields.
- Helps auto-remove expired data without manual cleanup.
Full Transcript
This visual execution shows how MongoDB TTL indexes work to auto-delete documents after a set time. First, a TTL index is created on a date field, like 'lastAccess'. MongoDB then periodically checks each document's date plus the TTL value against the current time. If the document is expired, it is deleted; otherwise, it is kept. The execution table traces documents with different lastAccess times and shows when they are deleted or kept. Key points include that TTL checks happen periodically, not instantly, and TTL indexes only work on date fields. This helps keep collections clean by removing old data automatically.