Skip to content

Instantly share code, notes, and snippets.

@bri-bri
Last active December 23, 2015 06:59
Show Gist options
  • Save bri-bri/6597826 to your computer and use it in GitHub Desktop.
Save bri-bri/6597826 to your computer and use it in GitHub Desktop.
For Sachin.
function convertObjectKeyToArray(obj,key) {
if( obj.hasOwnProperty(key) ) {
if( obj[key].constructor == Object ) {
new_array = [];
for ( objkey in obj[key] ) {
new_array.push(obj[key][objkey]);
}
obj[key] = new_array;
} else if(obj[key].constructor != Array ) {
print("{" + obj._id + "} has type " + typeof obj[key]+ " for key: " + key );
}
}
return obj;
}
// Example useage.
db.profiles.find().forEach( function( profile ) {
new_profile = convertObjectKeyToArray(profile,"INSERT SEARCH KEY");
if( new_profile != null ){
db.profiles.save(new_profile);
} else {
print ("{"+profile._id+"} had a problem.");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment