On a monthly basis, I want to delete all documents older than 1 month from one of my collections. An example document from the collection looks something like this:
{
_id: ObjectId("56e75f1ce851695805ead8ae"),
createDate: ISODate("2016-03-15T01:02:20.821Z"),
foo: "bar"
}
I want to have this task be automatically scheduled to happen so it requires no human input--say, as a cron job--and so I would like for it to have as little logic as possible.
How would I query for documents where the createDate is older than 1 month? I had thought to try something like this:
db.col.find({createDate: {$lt: ISODate(ISODate().getTime() - 1000*3600*24*30)}});
... where I create a new ISODate with the current time minus 1 month of milliseconds, but this isn't returning any data. It's also kinda ugly, even if it did work. How would I go about accomplishing this?