In the previous recipe, Expiring documents after a fixed interval using the TTL index, we have seen how documents can expire after a fixed time period. However, there can be some cases where we might want to have documents expiring 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 that the document can expire and it might be different for different documents.
For this recipe, we will create a collection called ttlTest2
. We will require a server to be up and running. Refer to the Installing single node MongoDB recipe from Chapter 1, Installing and Starting the Server for instructions on how to start the server. Start the shell with the TTLData.js
script loaded. This script is available on the Packt website for download. To know how to start the shell with a script preloaded, refer to the Connecting to a single node in the Mongo shell with JavaScript recipe in Chapter 1, Installing and Starting the Server.
addTTLTestData2
method. Execute the following on the mongo shell:> addTTLTestData2()
ttlTest2
collection as follows:> db.ttlTest2.createIndex({expiryDate :1}, {expireAfterSeconds:0})
find
query to view the three documents in the collection:> db.ttlTest2.find()
Let's start by opening the TTLData.js
file and see what is going on inside it. Our method of interest 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
fields set to 5
, 4
, and 7
minutes after the current time, respectively. Note that this field has a future date, unlike the date given in the previous recipe, where it was a creation date.
Next, we create an index: db.ttlTest2.createIndex({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, then this is the time in seconds that has elapsed after a base time when the document will be deleted from the collection by Mongo. This base time is the value held in the field that the index is created on (createTime
, as in the previous recipe). If this value is zero, then the date value that the index is created on (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 lot of 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 scenarios, a TTL index is not helpful and we might have to write an external job ourselves that does this work. Such a job could also read the collection for a range of documents, add them to the target collection, and delete them from the source collection. The folks at MongoDB have already planned to release a feature that addresses this issue.