Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Created October 19, 2015 11:45
Show Gist options
  • Save gitfvb/cd4907a19b5e2c1f6e33 to your computer and use it in GitHub Desktop.
Save gitfvb/cd4907a19b5e2c1f6e33 to your computer and use it in GitHub Desktop.
remove the 1000 first entries of the mongodb collection 'foo' by id
// count all the records first
db.getCollection('foo').find({}).count();
// save the the first 1000 entries ids to an array
removeIdsArray = db.getCollection('foo').find({})
.limit(1000)
.toArray()
.map(function(doc) { return doc._id; });
// remove them by going through that array
db.getCollection('foo').remove({_id: {$in: removeIdsArray}});
// count the records again
db.getCollection('foo').find({}).count();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment