BsonValue and GUIDs (C# driver)

1,596 views
Skip to first unread message

John Wood (maverin)

unread,
Jun 1, 2012, 3:07:38 PM6/1/12
to mongod...@googlegroups.com
If I use BsonValue.Create and pass in a Guid, it adds it as binary data. I can't see a BsonGuid subclass to use - so how do I store a GUID?

craiggwilson

unread,
Jun 1, 2012, 3:29:53 PM6/1/12
to mongod...@googlegroups.com
Guids are stored in binary form.  If you look at the bson specification here:  http://bsonspec.org/#/specification, you'll see UUID as listed under binary, subtype 3.  In actuality, there was some inconsistencies with how different drivers stored the guid byte order.  Because of this, there is another subtype, 4, which is used to store uuids in network byte order, consistently across all drivers.  So, depending on the byte order you want/need, you'll need to specify that at the global level using BsonDefaults.GuidRepresentation.  The default is CSharpLegacy for obvious reasons, but if you are starting a new database, we highly suggest you change it to Standard.

John Wood (maverin)

unread,
Jun 1, 2012, 3:32:19 PM6/1/12
to mongod...@googlegroups.com
Ok thanks - does that effect my ability to use records that contain GUIDs in the aggregate framework? Thought I read something about that somewhere...

craiggwilson

unread,
Jun 1, 2012, 3:47:37 PM6/1/12
to mongod...@googlegroups.com
I don't believe so (although I could be wrong).  The aggregation framework is most likely comparing them in binary form, so as long as you are consistent, you should be fine.  If you are doing range queries on Guids, then depending on how you choose to store your guids, then byte order obviously matters.  I'd suggest you use Standard format if you are starting new and if there is an issue in the aggregate framework, we'll create a bug and get it fixed.

Robert Stam

unread,
Jun 1, 2012, 3:53:14 PM6/1/12
to mongod...@googlegroups.com
There's an open server JIRA related to BinData in the aggregation framework:


so at the moment GUIDs couldn't flow through an aggregation pipeline because they are stored as BinData.

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

John Wood (maverin)

unread,
Jun 1, 2012, 4:00:47 PM6/1/12
to mongod...@googlegroups.com
Ok, it probably doesn't matter - it's unlikely you'd ever want to aggregate GUIDs in any way, their values are pretty meaningless other than being unique. Thanks.

John Wood (maverin)

unread,
Jun 1, 2012, 4:02:10 PM6/1/12
to mongod...@googlegroups.com
One more question - can these binary represented GUIDs be used in indexes without any problem / performance overhead? Or would using a string be better?

Robert Stam

unread,
Jun 1, 2012, 4:03:16 PM6/1/12
to mongod...@googlegroups.com
It's actually more limiting than that. I should have written:

...***documents containing Guids*** can't flow through an aggregation pipeline because they are stored as BinData.

The mere presence of a Guid in a document will prevent you from using the aggregation framework with that document.

Robert Stam

unread,
Jun 1, 2012, 4:06:06 PM6/1/12
to mongod...@googlegroups.com
Regarding indexes: the only performance implication is that GUIDs are slightly larger than ObjectIds, so your indexes will be slightly larger also.

And storing GUIDs as strings would be roughly twice the size of a binary GUID.

John Wood (maverin)

unread,
Jun 1, 2012, 4:06:55 PM6/1/12
to mongod...@googlegroups.com
Ouch. Ok, guess I'll be using strings then. Thanks for the clarification.


On Friday, June 1, 2012 4:03:16 PM UTC-4, Robert Stam wrote:
It's actually more limiting than that. I should have written:

...***documents containing Guids*** can't flow through an aggregation pipeline because they are stored as BinData.

The mere presence of a Guid in a document will prevent you from using the aggregation framework with that document.

Reply all
Reply to author
Forward
0 new messages