Skip to content

Instantly share code, notes, and snippets.

@flavianmissi
Created September 27, 2012 20:28
Show Gist options
  • Save flavianmissi/3796276 to your computer and use it in GitHub Desktop.
Save flavianmissi/3796276 to your computer and use it in GitHub Desktop.
mongodb "migration" script
// changes the data in db.collection[from] to
// db.collection[to] and deletes db.collection[from]
// how can I force the execution of the variables from and to?
function changeFieldKey(collection) {
var docs = db[collection].find({"from-key": {$exists: true}});
for (var i = 0; i < docs.length(); i++) {
print(docs[i])
docs[i]["to-key"] = docs[i]["from-key"];
delete docs[i]["from-key"];
print(docs[i])
db[collection].save(docs[i]);
}
}
changeFieldKey("apps")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment