Skip to content

Instantly share code, notes, and snippets.

@mathiash98
Last active September 4, 2017 19:09
Show Gist options
  • Save mathiash98/958a903e00b3bd58a00da05e779f7ba0 to your computer and use it in GitHub Desktop.
Save mathiash98/958a903e00b3bd58a00da05e779f7ba0 to your computer and use it in GitHub Desktop.
Bulk MongoDB edit key on all documents of collection, here: lowercase string except first letter
var bulk = db.postnummer.initializeOrderedBulkOp();
var counter = 0;
db.postnummer.find().forEach(function(data) {
var updoc = {
"$set": {}
};
var myKey = "kommune";
updoc["$set"][myKey] = data.kommune[0] + data.kommune.toLowerCase().substring(1);
// queue the update
bulk.find({
"_id": data._id
}).update(updoc);
counter++;
// Drain and re-initialize every 1000 update statements
if (counter % 1000 == 0) {
bulk.execute();
bulk = db.postnummer.initializeOrderedBulkOp();
}
})
// Add the rest in the queue
if (counter % 1000 != 0) bulk.execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment