Created
October 29, 2015 12:29
-
-
Save Stoffo/fd4a0a70be89e8d7ce15 to your computer and use it in GitHub Desktop.
Remove Documents older than x days in MongoDB
This file contains 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
3 years after, this just helped, it took time to understand why $lt was used instead of $gt.
Thanks for this.