Skip to content

Instantly share code, notes, and snippets.

@piuggi
Created December 23, 2014 08:15
Show Gist options
  • Save piuggi/8261261db1aa531165a3 to your computer and use it in GitHub Desktop.
Save piuggi/8261261db1aa531165a3 to your computer and use it in GitHub Desktop.
Update all documents in mongo a collection with an index
// where db.collection = the mongo collection you are looking to update
// this could be adapted in many ways
var collection = db.collection; var i = 0; collection.find().forEach(function(doc){ print("updating index: "+i); collection.update({_id:doc._id},{$set:{index:i}}); i++ })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment