The documents in the MongoDB collection have the following schema (showing only relevant fields):
{
"TTLinSeconds" : 1800,
"lastUpdatedTimestamp" : ISODate("...")
}
I need to delete all documents where current timestamp is greater than lastUpdatedTimestamp plus the value stored in the TTLinSeconds field. Thanks in advance for your suggestions.
This might work (to delete field between 2017-01-25T00:30:00Z and 2017-01-26T23:59:00Z ):
db.collectionName.remove({
$and : [
{"lastUpdatedTimestamp": {
$gte: ISODate("2017-01-25T00:30:00Z"),
$lt : ISODate("2017-01-26T23:59:00Z")
},
{"TTLinSeconds" : value}
]
}
Suggestion:
I would recommend you to use the common naming convention for your variables.
Reference :
https://docs.mongodb.com/manual/reference/operator/update/currentDate/
You could do this using $where as:
db.collectionName.remove({$where:"new Date().getTime() > this.lastUpdatedTimestamp.getTime() + (this.TTLinSeconds * 1000)"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With