Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3685

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

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major - P3 Major - P3
    • None
    • 2.12.3
    • None
    • None
    • 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}");
              }
          }
      }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            dot0blu@gmail.com _BLU N/A
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: