Expire Data from Collections using TTL

makes_eat_time

In this posts we are going to talk about MongoDB “Time to Live” collections feature and how could help us on lot of common cases, for example when storing log data in our applications, or a user notifications history for a limited period of time. MongoDB will delete automatically these documents for you,  following your rules.

To support TTL there is a special index property that relies on a background thread in mongod that reads the target index date values and performs delete operations on those who have expired.

To enable TTL for a collection use ensureIndex() method to create an index with the expireAfterSeconds additional property.

The background task that removes expired documents runs every 60 seconds

Keep in mind that because the deletion process runs every 60 seconds, documents can have a life of +59 seconds once it is past their expiration date.

When mongod process is running we can see the performed operations by the db.currentOp () instruction

There are two posibilities when creating TTL indexes.

Expire Documents after a Certain Number of Seconds

To expire documents after a certain number of senconds, create a TTL index that holds a BSON date type or an array of BSON date-typed objects and specify the non-zero number of seconds in the expireAfterSeconds property.

For example, imagine a log collection with the following structure where createdAt field stores a new Date() value:

To expire automatically all log entries with a Live Time greater than 5 minutes should create a new index on the createdAt field like this:

MongoDB will automatically delete documents from log collections when the createdAt value is older than the createdAt value + expireAfterSeconds value.

When adding new documents to the log collection, set the createdAt value to the current time:

Expire Documents at a Certain Clock Time

To automatically remove documents at a Certain Clock Time you should perform the same index as before but setting expireAfterSeconds value to zero.

Mongod process will look for the documents where the expireAt fild is older than current time and will delete them.

In this case, when adding new documents to the log collections you should specify the expiration Date for each document as follows:

César Trigo

Backend Development Director @ Gigigo Group

You may also like...

Leave a Reply