Q58 [Scenario]: Audit logs must be retained for exactly 90 days and deleted automatically. How do you implement this efficiently?

Create a TTL index on the createdAt timestamp field with expireAfterSeconds: 7776000 (90 days). MongoDB's background thread will purge expired documents automatically without requiring cron jobs or application intervention.

Key Takeaways
  • TTL indexes eliminate external cleanup cron scripts.

  • Calculated in seconds ($90 ext{ days} imes 24 imes 3600 = 7,776,000 ext{ seconds}$).

Knowledge Check

How many seconds must be configured in expireAfterSeconds for a 90-day TTL index?