I'm currently learning MongoDb as a possible database option, and I'm having problems serializing Guid. At first, I thought it was a mistake in serializing C # serialization, but now I think it is rather a naive assumption on my part.
To help me convert Bson basebox views back and forth to Guids, I wrote a few small powershell functions to help:
function base64toguid { param($str); $b = [System.Convert]::FromBase64String($str); $hex = ""; foreach ($x in $b) { $hex += $x.ToString("x2"); } $g = new-object -TypeName System.Guid -ArgumentList $hex; return $g; } function guidtobase64 { param($str); $g = new-object -TypeName System.Guid -ArgumentList $str; $b64 = [System.Convert]::ToBase64String($g.ToByteArray()); return $b64; }
An example of a problem that occurs to me:
:) guidtobase64("53E32701-9863-DE11-BD66-0015178A5E3C"); ASfjU2OYEd69ZgAVF4pePA== :) base64toguid("ASfjU2OYEd69ZgAVF4pePA=="); Guid ---- 0127e353-6398-11de-bd66-0015178a5e3c
And from the mongo shell:
:) mongo MongoDB shell version: 1.6.5 connecting to: test > b = new BinData(3, "ASfjU2OYEd69ZgAVF4pePA=="); BinData(3,"ASfjU2OYEd69ZgAVF4pePA==") > b.hex(); 127e353639811debd66015178a5e3c >
So, as you can see, Guid, I will return, does not match what I have invested. My function and hex () return the same. If you compare the original with the result:
53E32701-9863-DE11-BD66-0015178A5E3C
0127e353-6398-11de-bd66-0015178a5e3c
You can see that the first 3 sets of hexadecimal pairs are reversed, but the last 2 sets are not. This makes me think that there is something about Guid.ToString () that I do not understand.
Can someone enlighten me please?