You can use an aggregation pipeline for converting the string column to date then proceed with the TTL index. Something like the pipeline below, but you will need to transfer the other columns as well. Try it on a test table first db.yourCollection.aggregate({$project: {converted_date: {$dateFromString: {dateString: "created"}}}}, {$out: "yourCollection"})
That was some neat explaining. I understand it in one go. Thank you.
Thanks, clear explanation!
I have a doubt, is there any way we can expire a field only not the whole document itself?
i dont think so just make thoes field in separete table and use a foreign key
thanks man ❤
How to add TTL on string date
example "created" : "2024-03-15T09:06:13.504+0000"
You can use an aggregation pipeline for converting the string column to date then proceed with the TTL index. Something like the pipeline below, but you will need to transfer the other columns as well. Try it on a test table first
db.yourCollection.aggregate({$project: {converted_date: {$dateFromString: {dateString: "created"}}}}, {$out: "yourCollection"})
@@vladsavecoding4111 I am below error while creating TTL index
MongoServerError: The 'expireAfterSeconds' option is supported on '_ts' field only