MongoDB

How to Work with MongoDB TTL Indexes

TTL indexes, also called Time-To-Live Indexes, are unique one-field indexes that MongoDB may use to delete records from collections periodically. Data expiry is helpful for some kinds of data, such as auto-generated information about events, logs, and session info, which should only be stored in databases for a limited period. Today’s guide will elaborate on a simple way to work with TTL indexes in MongoDB.

Example 01

Whenever you establish a TTL index on the specified field, an underlying process in MongoDB periodically verifies the index. Based on the value in the timestamp column, it recognizes records that have disappeared. MongoDB immediately eliminates these outdated records from the collection after a specified time.

In this example, we will elaborate the use of the TTL index on a whole new record that will be removed after a set time interval. The below find() MongoDB command has been used to retrieve and display all the records from the Dummy database. The query’s results are then printed to the console. A list of records with the fields of this query are the outcomes.

The first field _id is the unique identifier for a record. The ‘name’ field displays the name of a person, while the type field displays the individual’s classification (“emp” or “student”). The last field “sal” shows the individual’s salary.

For instance, the first document in the results contains ID 1, name “Ana”, type “emp”, and a salary of 500. Right now, we only have 10 records in the “Dummy” collection.

test> db.Dummy.find()

You must build a TTL index on a particular field in your data set to enable TTL capability. This field should include a timestamp or date indicating when the record should expire and be eliminated by default should be entered in this column. Typically, this field has the name ‘createdAt’ or a very related name. It’s time to create a TTL index on the Dummy collection for its new records to be added.

So, the createIndex() function of MongoDB has been utilized on the whole record to be created. This index query has been using two fields: ‘createdAt’, and ‘expreAfterSeconds’. The ‘createdAt’ field has been set to “1” to remove the record after a specific set of time. The ‘expireAfterSeconds’ field specifies the time in seconds for record expiration (e.g., 60 seconds equals 1 minute). The TTL index is named “createdAt_1”.

Test> db.Dummy.createIndex({ "createdAt": 1 }, { expireAfterSeconds: 40 })

If you want to look for all the applied indexes of a particular collection, you can do so by using the below-mentioned command. Within its output, it will display the indexes applied to the collection i.e. “Dummy”, within the indexSizes column as displayed.

Test> db.Dummy.stats()

After applying the MongoDB TTL index to the “Dummy” collection, we added two more records in the collection to observe its effect. These records contain the id 11 and 12, name as Morphy and Caty, type as “emp” and student, and sal field as 66 and 25. The acknowledgment shows that the insertion is successful at our end.

test> db.Dummy.insertMany([ { _id: 11, name: "Morphy", type: "emp", sal: 66 },

{ _id: 12, name: "Caty", type: "student", sal: 25 } ])

Within 60 seconds (1 minute ), we checked for the “Dummy” collection records once again via the find() function query. The output of the displayed query has been showing a total of 12 records existing in the “Dummy” collection.

test> db.Dummy.find()

After 1 minute timespan, check for the records of the Dummy collection once again via the find() function query attached below. Now, the output has been updated, and it displays the total of 10 records added at the start of the example i.e. new records have been removed successfully as per the TTL index.

test> db.Dummy.find()

Example 02

Let’s move on to another example of a TTL index application on the MongoDB collection. In this case, we have created a new collection named “Test” and inserted 5 records into it using the “insertMany” method of MongoDB. Each record consists of two fields: “name” and “des.” The “name” field contains the names of different individuals, while the “des” field contains a short description of those individuals in one line. The acknowledgment displays the successful insertion of these 5 records.

test> db.Test.insertMany([ { name: 'Mophy', des: 'I think he is no where to find' },

{ name: 'Cilian', des: 'A good movie actor with an aura' },

{ name: 'Ana', des: 'I like her condifence' },

{ name: 'Paul', des: 'A handsome man' },

{ name: 'Ken', des: 'A super model you can say' } ])

After successfully adding records to the “test” collection, we have been querying all its records using the “find” function. The query has been returning the 5 records, each with “name”, “des”, and an additional field “_id” i.e. auto-generated identifier for a record.

test> db.Test.find()

Now, the collection is ready to apply the TTL index to it. It’s important to note that the Compound TTL indexes are outdated in case of record expiration, so, we cannot apply them. Therefore, we will proceed with applying the Single-field TTL index by using the createIndex() function on the “Test” collection. The “createdAt” field has been set to “1” for the removal of records after the timespan set to 40 seconds. The execution of this query returns the name of a TTL index i.e. “createdAt_1.”

test> db.Test.createIndex({ "createdAt": 1 }, { expireAfterSeconds: 40 })

After creating this index, we have been inserting 5 more records in the “Test” collection using the same insertMany function. The acknowledged message is displayed.

test> db.Test.insertMany([ { des: 'Enginer with extra skills' },

{ des: 'A creative artist and writer' },

{ des: 'A pop in a Church' },

{ des: 'An owner of Instagram' },

{ des: 'owns Microsoft'} ])
[/c]c
<img class="wp-image-373831" src="https://linuxhint.com/wp-content/uploads/2023/09/word-image-373798-10.png" />

Within 40 seconds after the insertion of new 5 records, the records have no change as per the find() function query displayed below, which shows a total of 10 records.
[cc lang="apache" width="100%" height="100%" escaped="true" theme="blackboard" nowrap="0"]
test> db.Test.find()

If it doesn’t reflect any changes, you can close and run your MongoDB application to see the effect of TTl indexes. So, we used the “find” function query to retrieve records from the “Test” collection. The newly inserted five records have been removed from the collection after 40 seconds have passed, as displayed in the output.

test> db.Test.find()

Conclusion

The introductory paragraph elaborates on the sole purpose of using TTL indexes to update MongoDB records regularly. After that, the code examples have been supporting the introduction of this guide i.e. by applying the TTL indexes on collections to remove their newly inserted documents.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.