Automatically Age Out MongoDB Collection Documents

One of the common scenarios in working with a database is to automatically prune some tables based on time. For example, you might have a jobs table to track background jobs in your product and you only want to keep jobs for the last hour. How it’s typically implemented is that you end up writing a daemon process that runs periodically and garbage-collects your tables. Given this is such a common process, the folks at MongoDB have built this functionality into the database which can be leveraged in your MongoDB deployments! It’s called “TTL indexes”. Here’s how you can use TTL indexes in two simple steps:

  1. Step 1: Add a Date Field

    Add a date field to your document to indicate the age of the document. MongoDB will use this field to determine if your document is expired and needs to be removed. If you wish to keep the document around longer, just update this document with an updated date. In the example below I have added a “creationTime” field to my jobs collection:

    db.jobs.insert( {
       "name" : testjob
       "creationTime": new Date('Oct 30, 2013: 11:00:00'),
       "type": 2,
    } )
  2. Step 2: Add a TTL Index

    Add a TTL index to your collection on this field. In this example below, we’ll use an expireAfterSeconds value of 3600. This will expire jobs after every hour:

    db.jobs.ensureIndex( { "creationTime": 1 }, { expireAfterSeconds: 3600 } )

    The TTL daemon run every 60 seconds, so your document will be deleted within approximately 60 seconds of expiry. Another advantage of TTL indexes is that they also behave like normal indexes – so you can query on the date field and the query plan will use the index. For more details, refer to the MongoDB documentation on TTL indexes.


Dharshan is the founder of ScaleGrid.io (formerly MongoDirector.com). He is an experienced MongoDB developer and administrator. He can be reached for further comment at @dharshanrg


0 Shares
+1
Tweet
Share
Share
Pin