Skip to content

Instantly share code, notes, and snippets.

@devdazed
Created July 22, 2011 18:32
Show Gist options
  • Save devdazed/1100068 to your computer and use it in GitHub Desktop.
Save devdazed/1100068 to your computer and use it in GitHub Desktop.
Show the difference between storing strings as BSON and symbols.
require 'mongo'
foo = ['this', 'that']
bar = [:this, :that]
a = {:foo => ['this', 'that']}
b = {:foo => [:this, :that]}
bson_a = BSON.serialize(a).to_s
#=> "'\x00\x00\x00\x04foo\x00\x1D\x00\x00\x00\x020\x00\x05\x00\x00\x00this\x00\x021\x00\x05\x00\x00\x00that\x00\x00\x00"
bson_b = BSON.serialize(b).to_s
#=> "'\x00\x00\x00\x04foo\x00\x1D\x00\x00\x00\x0E0\x00\x05\x00\x00\x00this\x00\x0E1\x00\x05\x00\x00\x00that\x00\x00\x00"
bson_a == bson_b
#=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment