Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

ttl

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.

like image 815
user2833162 Avatar asked Oct 01 '13 01:10

user2833162


1 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 })
like image 183
Daniel Coupal Avatar answered Oct 19 '22 05:10

Daniel Coupal