Using GuidRepresentation.Standard makes it impossible to retrieve Documents by Id in C#

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Gone away
    • Priority: Major - P3
    • None
    • Affects Version/s: 2.12.3
    • Component/s: None
    • None
    • Environment:
      MongoDb Server 4.4.6
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      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}");
              }
          }
      }
      

            Assignee:
            Unassigned
            Reporter:
            _BLU N/A
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: