There is a particular task I want to accomplish, but I am not finding any particular way to do that. Let\'s say I have an app that is used to send mails. I keep a record of thes
I would just add another field. It's not that much data:
{
'_id' : 123456789,
'createdDate' '',
'scheduledDate' : '
Add a TTL index of 0 seconds to the expires field. Don't add a TTL to the other dates.
The following examples are in Mongoose (an ORM for Node) but the ideas should carry over to whatever framework you're using:
You could add a default value to the expires field with the value of 'created date + 2 days'.
{
type: Date,
default: function() {
return new Date(Date.now() + 1000*60*60*24*2);
}
}
Just set the date explicitly:
myNewDocument.expires = new Date( scheduled + ... );
Or change the function that sets the default value:
function() {
if(this.get("type") === 2) {
return scheduled_date_plus_2_days;
} else {
return created_date_plus_2_days;
}
}
Set the field to NULL:
myNewDocument.expires = null;
This way, you have the ability to choose a different expiry for normal emails, important emails, scheduled ones, etc. If you're on time, you can even set the expires field to NULL to cancel the automatic deletion.
The only caveat is that if you change the scheduled time, you'll need to update the expires field.