Created
August 7, 2012 20:58
-
-
Save alexandrerocco/3289289 to your computer and use it in GitHub Desktop.
Migrate a mongodb collection to another structure (eg. rename fields)
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
// 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