Expiring documents at a given time using the TTL index

In the previous recipe, we saw how documents can be expired after a fixed time period. However, there can be some cases where we might want to have documents that expire at different times. This is not what we saw in the previous recipe. In this recipe, we will see how we can specify the time at which a document can be expired (it might be different for different documents).

Getting ready

For this recipe, we will create a collection called ttlTest2. We will require a server to be up and running. Refer to the Single node installation of MongoDB recipe in Chapter 1, Installing and Starting the MongoDB Server, to learn how to start the server. Also, start the shell with the TTLData.js script loaded. This script will be available on the book's website for download. To know how to start the shell with a script reloaded, refer to the Connecting to a single node from the Mongo shell with a preloaded JavaScript recipe in Chapter 1, Installing and Starting the MongoDB Server.

How to do it…

  1. Load the required data in the collection using the addTTLTestData2 method. Execute the following command on the Mongo shell:
    > addTTLTestData2()
    
  2. Now, create the TTL index on the ttlTest2 collection:
    > db.ttlTest2.ensureIndex({expiryDate :1}, {expireAfterSeconds:0})
    
  3. Execute the following find query to view the three documents in the collection:
    > db.ttlTest2.find()
    
  4. Now, after approximately 4, 5, and 7 minutes, see the documents with IDs 2, 1, and 3, respectively, getting deleted.

How it works…

Let's start by opening the TTLData.js file and seeing what must be going on in it. Our method for this recipe is addTTLTestData2. This method simply creates three documents in the tllTest2 collection with _id of 1, 2, and 3, with their exipryDate field set to 5, 4, and 7 minutes, respectively, after the current time. Note that this field has a future date, unlike the date given in the previous recipe where it was a creation date.

Next, we will create an index:

> db.ttlTest2.ensureIndex({expiryDate :1}, {expireAfterSeconds:0})

This is different from the way we created the index for the previous recipe, where the expireAfterSeconds field of the object was set to a non-zero value. This is how the value of the expireAfterSeconds attribute is interpreted. If the value is non-zero, that is, the time in seconds elapsed after a base time, then the document will be deleted from the collection by Mongo. This base time is the value held in the field on which the index is created (createTime, as in the previous recipe). If this value is 0, the date value on which the index is created (expiryDate in this case) will be the time when the document will expire.

To conclude, TTL indexes work well if you want to delete the document upon expiry. There are quite a few cases where we might want to move the document to an archive collection where the archived collection might be created based on, say, the year and month. In any such scenario, the TTL index is not helpful, and we might ourselves have to write an external job that does this work or reads the collection for a range of documents, adds them to the target collection, and deletes them from the source collection. JIRA (https://jira.mongodb.org/browse/SERVER-6895) is already open to address this issue. You might want to keep an eye on JIRA for further development on it.

There's more…

In this and the previous recipe, we looked at what TTL indexes are and how to use them. However, what if after creating a TTL index we want to modify it to change the value of the expireAfterSeconds value? It is possible using the collMod option. See more on this option in Chapter 4, Administration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset