Element ordering in BSON documents

264 views
Skip to first unread message

sptrakesh

unread,
Dec 12, 2012, 12:27:39 PM12/12/12
to bs...@googlegroups.com
Hello,

I do not see any mention of it in the specifications, but are two BSON documents considered identical if they have the exact same elements regardless of ordering of the elements?  I need this to properly implement my operator == for BSON documents.

Thanks
Rakesh

Gary Murakami

unread,
Dec 13, 2012, 1:41:49 PM12/13/12
to bs...@googlegroups.com
What language are you using?  BSON by specification is order preserving.  However, mapping into a data structure is language and/or driver dependent.  Here's what Ruby and the 10gen Ruby driver does, in Ruby 1.9.3 where Hash is order preserving.

$ irb
1.9.3-p327 :001 > require 'bson'
 => true
1.9.3-p327 :002 > { a: 1, b: 2 } == { b: 2, a: 1 }
 => true
1.9.3-p327 :003 > BSON::OrderedHash[ a: 1, b: 2 ] == BSON::OrderedHash[ b: 2, a: 1 ]
 => false

BSON::OrderedHash was needed for Ruby 1.8 where Hash is not order preserving.  The above difference is explicitly coded in the Ruby driver.  Drivers for other languages may differ, but I haven't done an explicit survey.  If you do one, I'd be interested in the results, or you could make a request to me/us via JIRA ticket.

Blessings,

-Gary

sptrakesh

unread,
Dec 19, 2012, 10:25:23 AM12/19/12
to bs...@googlegroups.com
Hi Gary,

Thanks for the clarification about the BSON specification mandating order perservation.  The question came up for my own C++ API for BSON.  I had originally implemented the == operator to ensure ordering is also considered but was then intrigued by the possibility of "equivalent" documents.  I decided in favour of also implementing a isEquivalentTo method in the document and let callers worry about what kind of equality check they wish to perform.

Rakesh
Reply all
Reply to author
Forward
0 new messages