A TTL (Time To Live) index in MongoDB automatically deletes documents from a collection after a specified amount of time has passed since a date field's value.
Click to reveal answer
beginner
How do you create a TTL index on a field named 'createdAt' that expires documents after 3600 seconds?
Use the command: db.collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })
Click to reveal answer
beginner
Which data type must the field used for a TTL index be?
The field must be a date type (ISODate) for TTL indexes to work correctly.
Click to reveal answer
intermediate
What happens if you update the date field used in a TTL index?
Updating the date field resets the countdown for document expiry based on the new date value.
Click to reveal answer
intermediate
Can TTL indexes be used to expire documents based on a timestamp in the future?
No, TTL indexes expire documents based on the time elapsed since the date field's value, so the date must be in the past or present.
Click to reveal answer
What does the 'expireAfterSeconds' option specify in a TTL index?
AThe number of seconds to keep the index active
BThe number of seconds before documents are indexed
CThe number of seconds to delay index creation
DThe number of seconds after which documents expire
✗ Incorrect
The 'expireAfterSeconds' option sets how many seconds after the date field's value a document should be deleted.
Which MongoDB command creates a TTL index on the 'lastModified' field with a 24-hour expiry?
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
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 B
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
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 D
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
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 on expireAt with expireAfterSeconds: 0 uses expireAfterSeconds: 0 on expireAt field to expire exactly at that time.
Final Answer:
Create TTL index on expireAt with expireAfterSeconds: 0 -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.