Created
March 6, 2014 17:46
-
-
Save normgraham/9395410 to your computer and use it in GitHub Desktop.
Copies documents from one collection to another, converting the types of field values along the way. This is a little hacky and isn't the kind of thing you'd normally want to do in JavaScript, but if you have no other convenient choices this script is a good example of the kinds of things you'll need to check. Invoke with something like {code} m…
This file contains hidden or 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 convertTypes = function (sourceNS, targetNS) { | |
| var stringProps = ["field1", "field2", "field3", "field4"]; | |
| var numberIntProps = ["field5", "field6"]; | |
| var dateProps = ["field7"]; | |
| source = db.getSiblingDB(sourceNS.split(".")[0])[sourceNS.split(".")[1]]; | |
| target = db.getSiblingDB(targetNS.split(".")[0])[targetNS.split(".")[1]]; | |
| var convertToString = function (prop) { | |
| if (typeof prop === 'string') | |
| return prop; | |
| else if (typeof prop === 'number' || typeof prop === 'boolean') | |
| return prop.toString(); | |
| else if (typeof prop === 'object' && prop !== null) { | |
| if (prop instanceof NumberInt || prop instanceof NumberLong || prop instanceof ObjectId) | |
| return prop.toString().split(/\(\"*|\"*\)/)[1]; | |
| else if (prop instanceof Date) | |
| return prop.toString(); | |
| } | |
| throw "Can't convert property to string"; | |
| } | |
| var convertToNumberInt = function (prop) { | |
| if (typeof prop === 'string' || typeof prop === 'number' || typeof prop === 'boolean') | |
| return new NumberInt(prop); | |
| else if (typeof prop === 'object' && prop !== null && (prop instanceof NumberInt || prop instanceof NumberLong)) | |
| return new NumberInt(prop); | |
| throw "Can't convert property to NumberInt"; | |
| } | |
| var convertToDate = function (prop) { | |
| if (typeof prop === 'string' || typeof prop === 'number' || typeof prop === 'boolean') | |
| return new Date(prop); | |
| else if (typeof prop === 'object' && prop !== null) { | |
| if (prop instanceof Date) | |
| return prop; | |
| else if (prop instanceof NumberInt || prop instanceof NumberLong) | |
| return new Date(prop); | |
| } | |
| throw "Can't convert property to Date"; | |
| } | |
| var docCounter = 0; | |
| source.find().forEach(function (doc) { | |
| // Go through all the fields in the doc converting those found in | |
| // the sample document to the desired type. | |
| // Don't modify fields that weren't mentioned in the sample document | |
| for (var propName in doc) { | |
| try { | |
| if (stringProps.indexOf(propName) >= 0) | |
| doc[propName] = convertToString(doc[propName]); | |
| else if (numberIntProps.indexOf(propName) >= 0) | |
| doc[propName] = convertToNumberInt(doc[propName]); | |
| else if (dateProps.indexOf(propName) >= 0) | |
| doc[propName] = convertToDate(doc[propName]); | |
| } | |
| catch (err) { | |
| print(err + ": " + propName + ", _id: " + doc._id); | |
| } | |
| } | |
| try { | |
| target.insert(doc); | |
| } | |
| catch (err) { | |
| print("Unable to insert _id: " + doc._id); | |
| } | |
| if (++docCounter % 20000 == 0) | |
| print("Documents converted: " + docCounter); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment