Automatically Age Out MongoDB® Collection Documents

2 min read
Automatically Age Out MongoDB® Collection Documents

SHARE THIS ARTICLE

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:

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

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.

For more information, please visit www.scalegrid.io. Connect with ScaleGrid on LinkedIn, X, Facebook, and YouTube.
Table of Contents

Stay Ahead with ScaleGrid Insights

Dive into the world of database management with our monthly newsletter. Get expert tips, in-depth articles, and the latest news, directly to your inbox.

Related Posts

blog-feature-img_whats-new-at-scalegrid

What’s New at ScaleGrid – September 2024

At ScaleGrid, we’re always pushing the boundaries to offer more flexibility and scalability to our customers. Over the past few...

Managing PostgreSQL® High Availability – Part I: PostgreSQL Automatic Failover

Managing High Availability (HA) in your PostgreSQL hosting is very important to ensuring your database deployment clusters maintain exceptional uptime...

RabbitMQ Security Compliance - ScaleGrid

RabbitMQ Security and Compliance

Follow fundamental procedures in authentication, encryption, and commitment to RabbitMQ security protocols to protect your RabbitMQ system and secure messages....

NEWS

Add Headline Here