Best way to store a GUID?

837 views
Skip to first unread message

GWBasic

unread,
Jan 29, 2010, 1:10:12 PM1/29/10
to mongodb-csharp
Any insight into the best way to store a GUID? I'm currently
converting to/from string; but are other approaches better? For
example, would I be better off using a byte array, int array, object
with two longs, ect, ect?

Joel Lucsy

unread,
Jan 29, 2010, 1:10:56 PM1/29/10
to mongodb...@googlegroups.com
System.Guid
--
Joel Lucsy
"The dinosaurs became extinct because they didn't have a space program." -- Larry Niven

Seth Edwards

unread,
Jan 29, 2010, 1:13:12 PM1/29/10
to mongodb...@googlegroups.com
You might want to use the Binary type with a subtype of UUID.

On Fri, Jan 29, 2010 at 1:10 PM, GWBasic <goo...@andrewrondeau.com> wrote:

Sam Corder

unread,
Jan 29, 2010, 1:29:00 PM1/29/10
to mongodb...@googlegroups.com
I think the uuid sub type is really for the python uuid type and it isn't normally the same number of bytes as a guid.  It is probably safer to use UserDefined instead.  I'm not sure how querying on it would work out if you need to do that.  In that case converting it to hex and storing it as a string might workout better.  Also the Oid is pretty well guaranteed to be unique across machines and time so you could look at that as an option too.  You can generate your own with the OidGenerator class.

Michael Dirolf

unread,
Jan 29, 2010, 1:31:15 PM1/29/10
to mongodb...@googlegroups.com
I think Seth's suggestion is a good one. The UUID subtype is just for
storing UUIDs from any source - nothing Python specific there. If
you're storing UUIDs I'd use that. If you're just looking for a unique
id I'd check out ObjectId as Sam points out.

GWBasic

unread,
Jan 29, 2010, 8:40:34 PM1/29/10
to mongodb-csharp
Thanks for all your help

Unfortunately, I can't use OIDs. For now, the Guids come from
"elsewhere" and their generation is out of my control. (Data in Mongo
points to data elsewhere.)

I think I can summarize the two working approaches for Guids:

1: Use the Guid's ToByteArray() method, put that into a
MongoDB.Driver.Binary object, and set its SubType to Uuid. Upon
retrieval, use the Binary object's Bytes property to reconstruct the
Guid.
2: Use the Guid's ToString() method. Upon retrieval, pass the string
to Guid's constructor to reconstruct the Guid.

Any major problems with either approach?

On Jan 29, 10:31 am, Michael Dirolf <m...@dirolf.com> wrote:
> I think Seth's suggestion is a good one. The UUID subtype is just for
> storing UUIDs from any source - nothing Python specific there. If
> you're storing UUIDs I'd use that. If you're just looking for a unique
> id I'd check out ObjectId as Sam points out.
>
>
>
> On Fri, Jan 29, 2010 at 1:29 PM, Sam Corder <sa...@codeargyle.com> wrote:
> > I think the uuid sub type is really for the python uuid type and it isn't
> > normally the same number of bytes as a guid.  It is probably safer to
> > use UserDefined instead.  I'm not sure how querying on it would work out if
> > you need to do that.  In that case converting it to hex and storing it as a
> > string might workout better.  Also the Oid is pretty well guaranteed to be
> > unique across machines and time so you could look at that as an option too.
> >  You can generate your own with the OidGenerator class.
>

> > On Fri, Jan 29, 2010 at 1:13 PM, Seth Edwards <sethaedwa...@gmail.com>

Sam Corder

unread,
Jan 29, 2010, 11:02:44 PM1/29/10
to mongodb...@googlegroups.com
Nothing apparent.

Ken Egozi

unread,
Jan 30, 2010, 4:38:46 AM1/30/10
to mongodb...@googlegroups.com
I'd vote against the string.
a string representation of GUID is much larger than the guid itself. Since the whole idea is to keep as much data in-memory, you should prefer smaller representations IMO
--
Ken Egozi.
http://www.kenegozi.com/blog
http://www.delver.com
http://www.musicglue.com
http://www.castleproject.org
http://www.idcc.co.il - הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם

GWBasic

unread,
Jan 30, 2010, 1:54:21 PM1/30/10
to mongodb-csharp
Keeping Guids as strings in the database might make things easier in
situations where using the mongo console is anticipated*. Anyway; I
don't like storing numerical / binary data as strings; so I'm using
the Binary approach, and I'm happy that we can document it for other
C#/mongo developers.

* Not all databases are huge. A 10 gigabyte database is a cakewalk
for a desktop computer, but can still hold a lot of valuable data.
Sometimes developer convenience trumps efficiency when working with
these kinds of databases.

> Ken Egozi.http://www.kenegozi.com/bloghttp://www.delver.comhttp://www.musicglue.comhttp://www.castleproject.orghttp://www.idcc.co.il- הכנס הקהילתי הראשון למפתחי דוטנט - בואו בהמוניכם

Reply all
Reply to author
Forward
0 new messages