TTL indexes help automatically remove old data from your database after a set time. This keeps your data fresh and saves space without manual cleanup.
TTL indexes for auto-expiry in MongoDB
db.collection.createIndex({ <field>: 1 }, { expireAfterSeconds: <seconds> })The field must be a date type.
expireAfterSeconds sets how many seconds after the date the document expires.
createdAt date.db.sessions.createIndex({ "createdAt": 1 }, { expireAfterSeconds: 3600 })timestamp.db.logs.createIndex({ "timestamp": 1 }, { expireAfterSeconds: 86400 })This example creates a TTL index that deletes documents 10 seconds after their createdAt time. It inserts one document and shows it immediately.
use mydb // Create a TTL index on 'createdAt' field to expire documents after 10 seconds db.tempData.createIndex({ "createdAt": 1 }, { expireAfterSeconds: 10 }) // Insert a document with current date db.tempData.insertOne({ "name": "test", "createdAt": new Date() }) // Query documents immediately printjson(db.tempData.find().toArray())
TTL cleanup runs in the background every 60 seconds, so documents may not disappear exactly at the expiration time.
TTL indexes only work on fields with BSON date type.
You cannot update the TTL value without dropping and recreating the index.
TTL indexes automatically delete documents after a set time.
They help keep data storage clean without manual work.
Use a date field and expireAfterSeconds option to set expiry.
Practice
Solution
Step 1: Understand TTL index function
A TTL index is designed to remove documents automatically after a certain time period.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.Final Answer:
Automatically delete documents after a specified time -> Option AQuick Check:
TTL index = auto-delete documents [OK]
- Thinking TTL encrypts data
- Confusing TTL with backup or sorting
- Assuming TTL speeds up queries
createdAt that expires documents after 3600 seconds?Solution
Step 1: Recall correct TTL index syntax
The correct syntax uses createIndex with the option expireAfterSeconds set to the expiry time in seconds.Step 2: Match options with correct syntax
Only db.collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 }) uses createIndex and expireAfterSeconds correctly.Final Answer:
db.collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 }) -> Option AQuick Check:
expireAfterSeconds option = correct TTL syntax [OK]
- Using wrong option names like ttl or expireAfter
- Using non-existent createTTLIndex method
- Forgetting to specify expireAfterSeconds
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?Solution
Step 1: Understand TTL expiry timing
The TTL index deletes documents after expireAfterSeconds from the timestamp field value.Step 2: Calculate expiry for 3 minutes
3 minutes = 180 seconds, which is more than 120 seconds, so the document will be deleted.Final Answer:
The document is automatically deleted -> Option BQuick Check:
TTL expiry after 120s means deletion after 3 minutes [OK]
- Thinking document stays forever
- Assuming TTL archives instead of deletes
- Believing TTL causes insertion errors
expireAfterSeconds: 0 on a field expireAt, but documents are not being deleted. What is the likely cause?Solution
Step 1: Check TTL index requirements
TTL indexes require the indexed field to be a date type to calculate expiry.Step 2: Analyze why documents not deleted
If expireAt is not a date, TTL cannot determine expiry time, so documents stay.Final Answer:
The expireAt field is not a date type -> Option DQuick Check:
TTL needs date field type to delete documents [OK]
- Thinking expireAfterSeconds cannot be zero
- Believing TTL only works on _id
- Assuming MongoDB restart is needed
expireAt field. Which TTL index configuration achieves this?Solution
Step 1: Understand TTL with exact expiry date
Setting expireAfterSeconds to 0 means documents expire exactly at the date/time in the field.Step 2: Compare options for exact expiry
Only Create TTL index onexpireAtwithexpireAfterSeconds: 0uses expireAfterSeconds: 0 on expireAt field to expire exactly at that time.Final Answer:
Create TTL index on expireAt with expireAfterSeconds: 0 -> Option CQuick Check:
expireAfterSeconds: 0 = expire exactly at date field [OK]
- Using non-zero expireAfterSeconds for exact expiry
- Confusing createdAt with expireAt field
- Relying on manual background jobs unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
