Created
March 28, 2014 19:55
-
-
Save jamestyj/9841740 to your computer and use it in GitHub Desktop.
Remove documents in batches
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
// Note: Ensure there's an existing index, e.g.: | |
// db.assets.ensureIndex({ assetExpiration: 1 }) | |
// Since remove() doesn't support limit(), we can't do the following directly: | |
// db.assets.remove({ assetExpiration: { $lt: newDate() } }).limit(batchSize) | |
// So we do the following instead: | |
batchSize = 100 | |
removeMe = [] | |
db.assets.find({ assetExpiration: { $lt: newDate() } }, { _id: 1 }). | |
limit(batchSize). | |
forEach(function(doc) { removeMe.push(doc._id) }) | |
db.assets.remove({ _id: { $in: removeMe } }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment