MongoDb: After inserting a UUID with ruby, c# can't convert it to GUID
I am attempting to insert an object into mongoDB using ruby and retrieve it using c# and the NoRM driver.
All seemed to be progressing well until I wanted to use a Guid within my c# object.
I used the following code to set a UUID in ruby before inserting it into mongo (as suggested by this blog post http://blog.mikeobrien.net/2010/08/working-with-guids-in-mongodb-with-ruby.html):
BSON::Binary.new("d7b73eed91c549bfaa9ea3973aa97c7b", BSON::Binary::SUBTYPE_UUID)
When retrieving this object in c# the exception "Byte array for GUID must be exactly 16 bytes long." was thrown.
Using the administrative shell I inspected the contents of the object. The guid property had been set to
BinData(3,"ZDdiNzNl开发者_StackOverflow中文版ZWQ5MWM1NDliZmFhOWVhMzk3M2FhOTdjN2I=")
However if I inserted the same Guid using c# the guid property was set to
BinData(3,"7T6318WRv0mqnqOXOql8ew==")
Any ideas what I'm doing wrong?
I think that blog example is just wrong. It looks to me like you want the guid to be a hexstring, ie starting with "\xd7" (one byte) not "d7"
I tried this:
guidpack=guid.scan(/../).map {|e| e.to_i(16)}.pack('c*')
And checked the Base64 encoded size, it looks right now.
Base64.encode64 BSON::Binary.new(guidpack, BSON::Binary::SUBTYPE_UUID).to_s
=> "17c+7ZHFSb+qnqOXOql8ew==\n"
But the result doesn't exactly match what happens when you use C# above, so this might not be the right answer at all. (I'm not testing with mongo etc, just the bson gem, so can't check sorry)
精彩评论