MongoDB TTL does not work on atlas but works fine on localhost

I followed this article - https://docs.mongodb.com/manual/tutorial/expire-data/
I created the index on atlas first. It was added successfully. But the documents weren’t deleted as per expiry.
I used the same method on localhost mongo shell, and it worked fine.

I added this index

{ "expiresAt": 1 }, { expireAfterSeconds: 0 } 

where expiresAt is a Date object in my document.

This is the document I expected to be deleted.

    _id    :5f55f440058cdd3754907255
    name    :"first"
    language    :"Plain Text"
    content    :"Some text"
    createdAt    :2020-09-07T08:50:08.708+00:00
    expiresAt    :2020-09-07T08:51:08.709+00:00
    __v    :0

Hi @Mithil_Poojary,

The created index is not expected to work as the expireAfterSeconds is set to 0. See here:
To expire data after a specified number of seconds has passed since the indexed field, create a TTL index on a field that holds values of BSON date type or an array of BSON date-typed objects and specify a positive non-zero value in the expireAfterSeconds field. A document will expire when the number of seconds in the expireAfterSeconds field has passed since the time specified in its indexed field

Please specify a non zero positive integer.

How did you create the ttl index on atlas? Was via a shell or the data explorer?

Which version is your localhost deployment and what is the atlas version?

Best
Pavel

2 Likes

Hi @Pavel_Duchovny

Thanks for looking into this!

I followed the second half of the article here.
I created the index online on web version of atlas.
On my atlas dashboard, on top right I see version = 4.2.8.
On my Ubuntu localhost, mongo version is -> MongoDB shell version v4.4.0.

Hi @Mithil_Poojary,

Ok I see what you mean, sorry for overlooking.

Please provide db.collection.getIndexes() from the Atlas connection.

Can you confirm that the documents are still present in the collection. I am asking this since the TTL thread is running every min and until it runs documents may still exist but should be removed in the next time. Additionally, if the amount of documents to expire is large it can take time to clear all the batches.

Best regards,
Pavel

1 Like

Please hold on, I am setting up the mongocli to get this.

Yes they are still present, the document that I posted in the question has not been deleted, even though the time (in UTC) has passed a long back.

Ok I didn’t need the mongocli tool, and I was able to connect through mongodb shell. My collection name is models. This is what I got.

db.models.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "test.models"
	},
	{
		"v" : 2,
		"unique" : true,
		"key" : {
			"name" : 1
		},
		"name" : "name_1",
		"ns" : "test.models",
		"background" : true
	},
	{
		"v" : 2,
		"key" : {
			"expiresAt" : 1
		},
		"name" : "expiresAt_1",
		"ns" : "test.models"
	}
]

Hi @Mithil_Poojary

The created index was not created as TTL as it does not have the TTL clause therefore the documents were not deleted.

Please recreate the index and share the screenshot of how you create this index via the UI.

Please note that the { expireAfterSeconds: 0 } needs to be placed in the “options” section of the UI and will be probably ignored if placed in the main window of the fields specification.

Best regards,
Pavel

1 Like

Ok.

  1. Dropping my existing index by clicking Drop on expiresAt_1. Then clicking on Create Index on top right.
  2. Pasting { "expiresAt": 1 }, { expireAfterSeconds: 0 } inside Fields because there is already a placeholder there. Also, I read this article.

Oh is that so? Because this is what I was doing. I will retry this time by pasting inside options.

Is this looking right? @Pavel_Duchovny

Hi @Mithil_Poojary,

Oh is that so? Because this is what I was doing. I will retry this time by pasting inside options .

I see the confusion, the last posted image is also not properly defined.

Ok you need to place field name under FIELDS:

{ "expiresAt": 1 }

And under OPTIONS:

 { expireAfterSeconds: 0 }

If you look on the createIndex command you will see that there are 2 separate documents to place parameters. The UI mimic this structure therefore all fields specs are under FIELDS and any option is under OPTIONS.

Thanks,
Pavel

2 Likes

It worked! I have been trying since last 2 days. Thank you so very much for your time and patience.@Pavel_Duchovny :heart:
I am new to mongoDB and this thread alone has taught me quite a lot. :tada:

3 Likes

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