[CSHARP-3685] Using GuidRepresentation.Standard makes it impossible to retrieve Documents by Id in C# Created: 24/May/21  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: None
Affects Version/s: 2.12.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: _BLU N/A Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDb Server 4.4.6



 Description   

Hello,
when using
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
it is impossible to retrieve documents by id.

 

Full example that creates a document and tries to find it by the Guid:

using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
 
namespace MongoDbTestProject
{
    public class TestModel
    {
        [BsonId]
        public Guid Id;
 
        public string SomeString;
        public int SomeNumber;
    }
 
    internal class Program
    {
        public static void Main(string[] args)
        {
            BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
 
            MongoClient client = new MongoClient("mongodb://localhost:27017");
            var database = client.GetDatabase("test_db");
            var collection = database.GetCollection<TestModel>("tests");
 
 
            var id = Guid.NewGuid();
            var data = new TestModel
            {
                Id = id,
 
                SomeString = "Test",
                SomeNumber = 123
            };
 
            collection.InsertOne(data);
 
            var foundData = collection.FindSync(f => f.Id == id).FirstOrDefault();
 
            Console.WriteLine($"Found: {foundData}");
        }
    }
}



 Comments   
Comment by Mikalai Mazurenka (Inactive) [ 03/Jun/21 ]

Hi dot0blu@gmail.com

I can confirm that this is a bug.

I would like to highlight that you are using GuidRepresentationMode.V2, which is being deprecated (you can refer to the docs: MongoDB .NET driver reference: GuidRepresentationMode).
I know that it might be confusing, since V2 is a default mode, but that will change in one of the next releases.

So I would suggest to set the mode to V3, which makes your example work:

BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;

Another alternative to work around this issue is to set the GuidRepresentation in BsonDefaults:

BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;

This option is less preferable and is used instead of the serializer registration:

BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));

Comment by Mikalai Mazurenka (Inactive) [ 31/May/21 ]

Hi dot0blu@gmail.com

Thank you for reporting this issue. I can confirm that I am able to reproduce it.

We need some time to investigate it and will come back to you.

Generated at Wed Feb 07 21:45:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.