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.
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”.
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.
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.
{ _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.
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.
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.
{ 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.
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.”
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.
{ 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.
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.