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

message communication network

What Is RabbitMQ: Key Features and Uses

RabbitMQ is an open-source message broker that simplifies inter-service communication by ensuring messages are effectively queued, delivered, and processed across...

Intro to Redis Sharding

Grasping the concept of Redis sharding is essential for expanding your Redis database. This method involves splitting data over various...

Redis vs Memcached in 2024

Choosing between Redis and Memcached hinges on specific application requirements. In this comparison of Redis vs Memcached, we strip away...

NEWS

Add Headline Here