Introduction

In this blog post, we will discuss the differences between GUID and LUUID in MongoDB when working with C#. We will explore the default behavior of the MongoDB serializer and provide solutions to ensure compatibility between GUIDs and LUUIDs.

Understanding the Issue

When a C# GUID is saved in a MongoDB collection, it is stored as a LUUID. However, when retrieving this LUUID back to a C# GUID, a different value is returned. This is due to the default configuration of the internal MongoDB serializer, which treats LUUID as a legacy type that is not fully compatible with GUIDs.

Solution 1: Registering a GuidSerializer

To ensure compatibility between GUIDs and LUUIDs, you can register a GuidSerializer in the startup of your application. This will force the MongoDB serializer to save and retrieve a compatible UUID with your GUID.

Add the following line at the end of the Startup class:

BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
Code language: C# (cs)

This will ensure that the MongoDB serializer saves and retrieves GUIDs in a compatible format.

Solution 2: Retrieving Items Based on GUID

To retrieve an item from a collection based on a GUID, you need to make another change in the Startup class. The following method can be used:

private IFindFluent Find(CRILevelCriteria criteria)
{
    try
    {
        if (criteria == null)
            throw new ArgumentNullException(nameof(criteria));

        var filters = new List<FilterDefinition<CRILevelExportedEventLog>>();

        if (criteria.Id.HasValue)
            filters.Add(Builders<CRILevelExportedEventLog>.Filter.Where(x => x.EventId == criteria.Id.Value));

        FilterDefinition<CRILevelExportedEventLog> combinedFilters = Builders<CRILevelExportedEventLog>.Filter.And(filters);

        return Collection.Find(filters.Count <= 0 ? new BsonDocument() : combinedFilters);
    }
    catch (Exception ex)
    {
        Logger.Technical().Exception(ex).Log();
        throw;
    }
}
Code language: C# (cs)

To make this method work, update the Startup / Program class as follows:

BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;
Code language: C# (cs)

Please note that the second line is currently considered obsolete, but it is still necessary until the MongoDB driver for C# is updated and fixes this issue.

Conclusion

In this blog post, we discussed the issue of GUID vs LUUID in MongoDB when working with C#. We explored the default behavior of the MongoDB serializer and provided two solutions to ensure compatibility between GUIDs and LUUIDs. By registering a GuidSerializer and making the necessary changes in the Startup class, you can save and retrieve GUIDs in a compatible format.

For more information and detailed discussions on this topic, you can refer to the following sources:

https://www.codeproject.com/Articles/5274416/3-Best-Practices-for-GUID-data-in-MongoDB
https://stackoverflow.com/questions/1094061/how-do-i-use-a-guid-in-a-mongodb-shell-query
https://dev.to/kevsoft/storing-guids-as-strings-in-mongodb-with-c-3d5d
https://stackoverflow.com/questions/66378371/get-a-document-by-luuid
https://www.loginradius.com/blog/engineering/guid-query-through-mongo-shell/