-
-
Save evancauwenberg/d6e7cffd7976e85786dd8a6b1d39c579 to your computer and use it in GitHub Desktop.
Remove Documents older than x days in MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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