Skip to content

Instantly share code, notes, and snippets.

@SimonSimCity
Created October 13, 2016 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimonSimCity/22874eb86efb1804011cadea1e20fc9e to your computer and use it in GitHub Desktop.
Save SimonSimCity/22874eb86efb1804011cadea1e20fc9e to your computer and use it in GitHub Desktop.
Replaces a UUID that has been saved as BinData(0, "") as a UUID.
function decode_base64(s) {
var e={},i,k,v=[],r='',w=String.fromCharCode;
var n=[[65,91],[97,123],[48,58],[43,44],[47,48]];
for(z in n){for(i=n[z][0];i<n[z][1];i++){v.push(w(i));}}
for(i=0;i<64;i++){e[v[i]]=i;}
for(i=0;i<s.length;i+=72){
var b=0,c,x,l=0,o=s.substring(i,i+72);
for(x=0;x<o.length;x++){
c=e[o.charAt(x)];b=(b<<6)+c;l+=6;
while(l>=8){r+=w((b>>>(l-=8))%256);}
}
}
return r;
}
db.myDatabase.find(function() { return this.myUuidField.subtype() == 3 }).snapshot().forEach(function(elem) {
db.myDatabase.update(
{
_id: elem._id
},
{
$set: {
myUuidField: UUID(decode_base64(elem.myUuidField.base64()).replace(/-/gi, ''))
}
}
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment