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

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.

Answers


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 })

I have been thinking about using the build in TTL feature, but it's not easy to dynamically changing the expiration date

That's odd. Why would that be a problem? If your document has a field Expires, you can update that field at any time to dynamically prolong or shorten the life of the document.

Is there any downside just coding my own purging function based on "> certain_date" and run say once a day?

  1. You have to code, document and maintain it
  2. Deleting a whole lot of documents can be expensive and lead to a lot of re-ordering. It's probably helpful to run the purging more often

Minimizing the number of indexes is a good thing, but the question is whether it's really worth the effort. Only you can give an answer to this question. My advice is: start with something that's already there if any possible and come up with something better if and only if you really have to.


Need Your Help

Google Admin SDK Directory API User Privileges

google-oauth google-admin-sdk google-api-php-client

Can a normal user account in my domain access the function on Google OAuth2 directory service API to retrieve users list or data ?

Why does one loop take longer to detect a shared memory update than another loop?

c++ multithreading shared-memory latency spinlock

I've written a 'server' program that writes to shared memory, and a client program that reads from the memory. The server has different 'channels' that it can be writing to, which are just different