Bird
Raised Fist0
MongoDBquery~20 mins

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

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

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