Last active
September 4, 2017 19:09
-
-
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
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
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