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
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?
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.
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.
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.
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.
It worked! I have been trying since last 2 days. Thank you so very much for your time and patience.@Pavel_Duchovny
I am new to mongoDB and this thread alone has taught me quite a lot.