3

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,"ZDdiNzNlZWQ5MWM1NDliZmFhOWVhMzk3M2FhOTdjN2I=")

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?

Castrohenge
  • 8,525
  • 5
  • 39
  • 66

1 Answers1

2

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)

Ben Nagy
  • 66
  • 1
  • Thanks for the help. I initially thought this was incorrect because the first three groups of bytes we're in reverse order ("ed3eb7d7-c591-bf49" in c#, "d7b73eed-91c5-49bf" in ruby). However both saved into Mongo with the same value ("17c+7ZHFSb+qnqOXOql8ew=="). After stumbling upon this question http://stackoverflow.com/questions/5745512/how-to-read-a-net-guid-into-a-java-uuid I realised that c# stores the guid as little endian whilst ruby uses big endian. – Castrohenge Jul 12 '11 at 19:06