Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save evancauwenberg/d6e7cffd7976e85786dd8a6b1d39c579 to your computer and use it in GitHub Desktop.
Save evancauwenberg/d6e7cffd7976e85786dd8a6b1d39c579 to your computer and use it in GitHub Desktop.
Remove Documents older than x days in MongoDB
var date = new Date();
var daysToDeletion = 120;
var deletionDate = new Date(date.setDate(date.getDate() - daysToDeletion));
printjson(deletionDate);
var db = db.getSiblingDB('db')
db.getMongo().setSlaveOk();
printjson(db.messages.find({insertDate : {$lt : deletionDate}}).count());
//delete old Messages:
db.messages.remove({insertDate : {$lt : deletionDate}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment