0
0
MongoDBquery~5 mins

TTL indexes for auto-expiry in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a TTL index in MongoDB?
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
Which MongoDB command creates a TTL index on the 'lastModified' field with a 24-hour expiry?
Adb.collection.createTTLIndex({ lastModified: 1 }, 86400)
Bdb.collection.createIndex({ lastModified: 1 }, { expireAfterSeconds: 86400 })
Cdb.collection.createIndex({ lastModified: 1 }, { expireAfterMinutes: 1440 })
Ddb.collection.createIndex({ lastModified: 1 }, { expireAfterSeconds: 1440 })
What type of field is required for a TTL index to work properly?
ADate
BString
CNumber
DBoolean
If a document's date field used in a TTL index is updated to a newer date, what happens?
AThe document is never deleted
BThe document expires immediately
CThe expiry countdown resets based on the new date
DThe TTL index is removed
Can TTL indexes be used to expire documents exactly at a future date and time?
ANo, TTL indexes only expire documents based on elapsed time since the date field
BYes, but only if the date is stored as a string
CYes, TTL indexes support future expiry dates
DNo, TTL indexes expire documents immediately
Explain how TTL indexes work in MongoDB and how you would set one up to auto-delete documents after 1 hour.
Think about how MongoDB uses a date field and a time interval to remove old documents.
You got /4 concepts.
    What are the limitations and important considerations when using TTL indexes for auto-expiry in MongoDB?
    Consider data types, timing behavior, and how MongoDB cleans expired documents.
    You got /5 concepts.