Bird
Raised Fist0
MongoDBquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a TTL index in MongoDB?
easy
A. Automatically delete documents after a specified time
B. Encrypt documents for security
C. Create backups of collections
D. Improve query speed by sorting data

Solution

  1. Step 1: Understand TTL index function

    A TTL index is designed to remove documents automatically after a certain time period.
  2. Step 2: Compare options with TTL purpose

    Only Automatically delete documents after a specified time describes automatic deletion after time, which matches TTL index behavior.
  3. Final Answer:

    Automatically delete documents after a specified time -> Option A
  4. Quick Check:

    TTL index = auto-delete documents [OK]
Hint: TTL means auto-delete documents after time [OK]
Common Mistakes:
  • Thinking TTL encrypts data
  • Confusing TTL with backup or sorting
  • Assuming TTL speeds up queries
2. Which of the following is the correct way to create a TTL index on a field named createdAt that expires documents after 3600 seconds?
easy
A. db.collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })
B. db.collection.createIndex({ createdAt: 1 }, { ttl: 3600 })
C. db.collection.createTTLIndex({ createdAt: 1 }, 3600)
D. db.collection.createIndex({ createdAt: 1 }, { expireAfter: 3600 })

Solution

  1. Step 1: Recall correct TTL index syntax

    The correct syntax uses createIndex with the option expireAfterSeconds set to the expiry time in seconds.
  2. Step 2: Match options with correct syntax

    Only db.collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 }) uses createIndex and expireAfterSeconds correctly.
  3. Final Answer:

    db.collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 }) -> Option A
  4. Quick Check:

    expireAfterSeconds option = correct TTL syntax [OK]
Hint: Use expireAfterSeconds option in createIndex [OK]
Common Mistakes:
  • Using wrong option names like ttl or expireAfter
  • Using non-existent createTTLIndex method
  • Forgetting to specify expireAfterSeconds
3. Given a collection with documents having a timestamp field and a TTL index created as db.logs.createIndex({ timestamp: 1 }, { expireAfterSeconds: 120 }), what happens to a document inserted with { timestamp: new Date() } after 3 minutes?
medium
A. The document remains indefinitely
B. The document is automatically deleted
C. The document is archived but not deleted
D. The document causes an error on insertion

Solution

  1. Step 1: Understand TTL expiry timing

    The TTL index deletes documents after expireAfterSeconds from the timestamp field value.
  2. Step 2: Calculate expiry for 3 minutes

    3 minutes = 180 seconds, which is more than 120 seconds, so the document will be deleted.
  3. Final Answer:

    The document is automatically deleted -> Option B
  4. Quick Check:

    TTL expiry after 120s means deletion after 3 minutes [OK]
Hint: TTL deletes after expireAfterSeconds from date field [OK]
Common Mistakes:
  • Thinking document stays forever
  • Assuming TTL archives instead of deletes
  • Believing TTL causes insertion errors
4. You created a TTL index with expireAfterSeconds: 0 on a field expireAt, but documents are not being deleted. What is the likely cause?
medium
A. TTL indexes only work on _id field
B. expireAfterSeconds cannot be zero
C. You must restart MongoDB for TTL to work
D. The expireAt field is not a date type

Solution

  1. Step 1: Check TTL index requirements

    TTL indexes require the indexed field to be a date type to calculate expiry.
  2. Step 2: Analyze why documents not deleted

    If expireAt is not a date, TTL cannot determine expiry time, so documents stay.
  3. Final Answer:

    The expireAt field is not a date type -> Option D
  4. Quick Check:

    TTL needs date field type to delete documents [OK]
Hint: Ensure TTL field is a date type [OK]
Common Mistakes:
  • Thinking expireAfterSeconds cannot be zero
  • Believing TTL only works on _id
  • Assuming MongoDB restart is needed
5. You want documents in a collection to expire exactly at a specific date and time stored in the expireAt field. Which TTL index configuration achieves this?
hard
A. Create TTL index on createdAt with expireAfterSeconds set to document age
B. Create TTL index on expireAt with expireAfterSeconds: 3600
C. Create TTL index on expireAt with expireAfterSeconds: 0
D. Use a background job to delete documents at expireAt

Solution

  1. Step 1: Understand TTL with exact expiry date

    Setting expireAfterSeconds to 0 means documents expire exactly at the date/time in the field.
  2. Step 2: Compare options for exact expiry

    Only Create TTL index on expireAt with expireAfterSeconds: 0 uses expireAfterSeconds: 0 on expireAt field to expire exactly at that time.
  3. Final Answer:

    Create TTL index on expireAt with expireAfterSeconds: 0 -> Option C
  4. Quick Check:

    expireAfterSeconds: 0 = expire exactly at date field [OK]
Hint: Use expireAfterSeconds: 0 for exact expiry date [OK]
Common Mistakes:
  • Using non-zero expireAfterSeconds for exact expiry
  • Confusing createdAt with expireAt field
  • Relying on manual background jobs unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard