Skip to content

Instantly share code, notes, and snippets.

@jamestyj
Created March 28, 2014 19:55
Show Gist options
  • Save jamestyj/9841740 to your computer and use it in GitHub Desktop.
Save jamestyj/9841740 to your computer and use it in GitHub Desktop.
Remove documents in batches
// 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