What's the benefit of mongodb's ttl collection? vs purging data from a housekeeper?

user2833162 picture user2833162 · Oct 1, 2013 · Viewed 8.4k times · Source

I have been thinking about using the build in TTL feature, but it's not easy to dynamically changing the expiration date. Since mongodb is using a background task purging the data. Is there any downside just coding my own purging function based on "> certain_date" and run say once a day? This way, I can dynamically changing the TTL value, and this date field won't have to be single indexed. I can reuse this field as part of the complex indexing to minimize number of indexes.

Answer

Daniel Coupal picture Daniel Coupal · Jan 31, 2014

There are 2 ways to set the expiration date on a TTL collection:

  1. at a global level, when creating the index
  2. per document, as a field in the document

Those modes are exclusive.

Global expiry

If you want all your documents to expire 3 months after creation, use the first mode by creating the index like the following:

db.events.ensureIndex({ "createdAt": 1 }, { expireAfterSeconds: 7776000 })

If you later decide to change the expiry to "4 months", you just need to update the expireAfterSeconds value using the collMod command:

db.runCommand({"collMod" : "events" , "index" : { "keyPattern" : {"createdAt" : 1 } , "expireAfterSeconds" : 10368000 } })

Per-document expiry

If you want to have every document has its own expiration date, save the specific date in a field like "expiresAt", then index your collection with:

db.events.ensureIndex({ "expiresAt": 1 }, { expireAfterSeconds: 0 })