Convert MongoDB’s UUID to .NET GUID

One of the form how MongoDB represents UUID is BinData of type 3 or 4. The post covers how to convert the Legacy UUID (LUUID) to the .NET GUID representation.

ID fields in MongoDB are usually represented in form of binary data:

{ "_id" : BinData(3, "MyIRAFVEd2aImaq7zN3u/w==") }

The sub-type 3 indicates that the BinData content is a legacy UUID.

In order to get the GUID representation, usual for .NET, use the following snippet:

public Guid ConvertBinData3ToGuid(string input)
{
    var bytes = Convert.FromBase64String(input);
    return new Guid(bytes);
}