Skip to content

Instantly share code, notes, and snippets.

@alexandrerocco
Created August 7, 2012 20:58
Show Gist options
  • Save alexandrerocco/3289289 to your computer and use it in GitHub Desktop.
Save alexandrerocco/3289289 to your computer and use it in GitHub Desktop.
Migrate a mongodb collection to another structure (eg. rename fields)
// you can copy the collection from one old db to the new one (if needed)
db.oldcollection.find().forEach(function(d) {db.getSiblingDB('anotherdb')['newcollection'].insert(d);});
// gets all the current docs
var docs = db.oldcollecion.find();
// for each old doc, insert it into the new collection
docs.forEach(function(old) { var doc = {_id:old._id, ac:old.action}; if(old.advertiserid) doc.adid=old.advertiserid; db.newcollection.insert(doc); });
// removes the old collection
db.oldcollection.drop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment