Skip to content

Instantly share code, notes, and snippets.

@sgoel
Forked from bri-bri/gist:6597826
Last active December 23, 2015 07:09
Show Gist options
  • Save sgoel/6598976 to your computer and use it in GitHub Desktop.
Save sgoel/6598976 to your computer and use it in GitHub Desktop.
function convertObjectKeyToArray(obj,key) {
if( obj.hasOwnProperty(key) ) {
if( Object.prototype.toString.call(obj[key]) === '[object Object]' ) {
new_array = [];
for ( objkey in obj[key] ) {
new_array.push(obj[key][objkey]);
}
obj[key] = new_array;
} else {
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