Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

TTL index not purging documents

I have set up a TTL index on one of the collections
key" :

{
    "updated_at" : 1.0
},
"name" : "updated_at_1",
"ns" : "xxx-yyy",
"expireAfterSeconds" : 180.0,
"sparse" : true,
"background" : true

It is scheduled to expire documents every 3 minutes

My TTLMonitor thread is scheduled to run every 300 secs - 5 minutes

 db.adminCommand({getParameter:1, ttlMonitorSleepSecs: 1});
{
	"ttlMonitorSleepSecs" : 300,
	"ok" : 1,
	"operationTime" : Timestamp(1589968984, 4),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1589968984, 4),
		"signature" : {
			"hash" : BinData(0,"YwsahwMVAlldgw3S02zksPGbVM0="),
			"keyId" : NumberLong("6799818280593784833")
		}
	}
}

I have checked the admin logs as well

db.setLogLevel(1, “index”);

TTL thread is running and also looking at my index, but it’s not purging the documents

{ updated_at: 1.0 } name: updated_at_1
2020-05-20T10:09:32.873+0000 D INDEX [TTLMonitor] deleted: 0

Example of one of the document I have in my collection

{
	"_id" : ObjectId("5ec4ede5f523db6eb8b98c7a"),
	"session_id" : "1587992244204",
	"user_id" : "XXXXXXX",
	"messages" : [
		
	],
	"created_at" : NumberLong(1589964261),
	"**updated_at**" : NumberLong(1589964263)
}

Now this updated_at is generated using epoch time in seconds. Please tell me how can I make it work?

Hi @Prateek_GUpta,

TTL Indexes currently only support expiration of data based on comparison with a BSON Date value for the expiry:

If the indexed field in a document is not a date or an array that holds a date value(s), the document will not expire.

If a document does not contain the indexed field, the document will not expire.

The BSON Date type is a signed 64-bit integer representing milliseconds after (or before) the epoch, so it should be straightforward to convert your existing values to the supported data type.

If you want to support expiration of documents based on field types or comparisons outside of what is provided in the built-in TTL index, an alternative approach would be writing your own script and scheduling using something like the cron utility (Linux/Unix operating systems) or Task Scheduler (Windows).

Regards,
Stennie

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.