Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert .NET Guid to MongoDB ObjectID

Tags:

c#

guid

mongodb

How can I convert a .NET GUID to a MongoDB ObjectID (in C#). Also, can I convert it back again to the same GUID from the ObjectID?

like image 291
Journeyman Avatar asked Apr 01 '11 13:04

Journeyman


People also ask

Is MongoDB ObjectId a GUID?

MongoDB | ObjectID() FunctionAn ObjectID is a GUID (Globally Unique Identifier). GUIDs are generated randomly via an algorithm to ensure uniqueness.

How does MongoDB generate ObjectId?

Basically, ObjectId is treated as the primary key within any MongoDB collection. It is generated automatically whenever we create a new document within a new collection. It is based on a 12-byte hexadecimal value as you can observe in the following syntax.

How is ObjectId generated?

ObjectID is automatically generated by the database drivers, and will be assigned to the _id field of each document. ObjectID can be considered globally unique for all practical purposes. ObjectID encodes the timestamp of its creation time, which may be used for queries or to sort by creation time.

What is GUID MongoDB?

GUIDs are often being used to identify custom objects created in software. Software developers very often explicitly store those identifiers in the database and do not rely on identifiers generated by the database system. MongoDB and the MongoDB drivers come with built-in support for the GUID/UUID data type.


2 Answers

You can't convert ObjectId into GUID and vice versa, because they are two different things(different sizes, algoritms).

You can use any type for mongoDb _id including GUID.

For example in official c# driver you should specify attribute [BsonId]:

[BsonId] public Guid Id {get;set;}  [BsonId] public int Id {get;set;} 

ObjectId:

A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored big endian unlike the rest of BSON. This is because they are compared byte-by-byte and we want to ensure a mostly increasing order.

GUID:

The value of a GUID is represented as a 32-character hexadecimal string, such as {21EC2020-3AEA-1069-A2DD-08002B30309D}, and is usually stored as a 128-bit integer

like image 90
Andrew Orsich Avatar answered Sep 19 '22 15:09

Andrew Orsich


FYI You can convert from an ObjectId to a Guid

    public static Guid AsGuid(this ObjectId oid)     {         var bytes = oid.ToByteArray().Concat(new byte[] { 5, 5, 5, 5 }).ToArray();         Guid gid = new Guid(bytes);         return gid;     }      /// <summary>     /// Only Use to convert a Guid that was once an ObjectId     /// </summary>     public static ObjectId AsObjectId(this Guid gid)     {         var bytes = gid.ToByteArray().Take(12).ToArray();         var oid = new ObjectId(bytes);         return oid;     } 
like image 34
MiddleTommy Avatar answered Sep 17 '22 15:09

MiddleTommy