Skip to content

Instantly share code, notes, and snippets.

@fsouza
Forked from flavianmissi/migration.js
Created October 4, 2012 19:37
Show Gist options
  • Save fsouza/3835899 to your computer and use it in GitHub Desktop.
Save fsouza/3835899 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