0
0
MongoDBquery~20 mins

TTL indexes for auto-expiry in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TTL Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What happens when a TTL index expires a document?

Consider a MongoDB collection with a TTL index set to expire documents 3600 seconds after the createdAt field. What is the expected output after the TTL monitor runs?

MongoDB
db.collection.find({})
ADocuments older than 3600 seconds based on <code>createdAt</code> are automatically deleted.
BDocuments are archived to another collection after 3600 seconds.
CDocuments are updated with a new timestamp after 3600 seconds.
DDocuments remain unchanged; TTL indexes only create an index but do not delete.
Attempts:
2 left
💡 Hint

Think about what TTL indexes are designed to do automatically in MongoDB.

📝 Syntax
intermediate
2:00remaining
Which command correctly creates a TTL index to expire documents after 24 hours?

Choose the correct MongoDB shell command to create a TTL index on the timestamp field that expires documents after 24 hours.

Adb.collection.createTTLIndex({ timestamp: 1 }, { expireAfterSeconds: 86400 })
Bdb.collection.createIndex({ timestamp: 1 }, { ttl: 86400 })
Cdb.collection.createIndex({ timestamp: 1 }, { expireAfterSeconds: 86400 })
Ddb.collection.createIndex({ timestamp: 1 }, { expireAfter: 86400 })
Attempts:
2 left
💡 Hint

Look for the correct option name for the expiration time in the index options.

optimization
advanced
2:00remaining
How to optimize TTL index performance for high write throughput?

You have a collection with a TTL index on createdAt and very high write volume. Which approach optimizes TTL index performance?

ACreate multiple TTL indexes on different date fields to distribute load.
BUse a single TTL index on <code>createdAt</code> and shard the collection by <code>createdAt</code>.
CAvoid TTL indexes and manually delete expired documents with a background job.
DUse a compound index with <code>createdAt</code> and a frequently updated field.
Attempts:
2 left
💡 Hint

Consider how sharding and indexing affect write performance and TTL expiration.

🔧 Debug
advanced
2:00remaining
Why does the TTL index not delete expired documents?

A TTL index was created on expireAt with expireAfterSeconds: 0, but expired documents remain. What is the likely cause?

AThe <code>expireAt</code> field is not a date type or missing in documents.
BThe TTL monitor runs only once a day, so documents are not deleted yet.
CThe <code>expireAfterSeconds</code> value must be greater than zero.
DTTL indexes only work on the <code>createdAt</code> field by default.
Attempts:
2 left
💡 Hint

Check the data type and presence of the indexed field in documents.

🧠 Conceptual
expert
3:00remaining
What is the behavior of TTL indexes with clustered collections?

In MongoDB, when using clustered collections, how does TTL index behavior differ compared to regular collections?

ATTL indexes can be created on any field, but expiration is delayed due to clustering.
BTTL indexes are not supported on clustered collections; expiration must be manual.
CTTL indexes automatically expire documents, but only if the clustered key is a date field.
DTTL indexes work the same, but the clustered key must be the TTL indexed field.
Attempts:
2 left
💡 Hint

Think about how clustered collections organize data and index requirements.