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

GuidRepresentation made obsolete while function appears impared and documentation remains out of date.



    • Bug
    • Status: Backlog
    • Major - P3
    • Resolution: Unresolved
    • 2.11.0
    • None
    • API, Bson, Documentation
    • None
    • All
    • Fully Compatible


      Linked to https://jira.mongodb.org/browse/CSHARP-3179

      The entirety of the commonly used BsonDefaults method of setting GuidRepresentation has been marked obsolete while the documentation still refers to it and neither the "obsolete" warning or xmldoc inform you of what the replacement is beyond the vague statement `[Obsolete("Configure serializers instead.")]`

      The following example will fail with `MongoDB.Driver.MongoCommandException: 'Command findAndModify failed: After applying the update, the (immutable) field '_id' was found to have been altered to _id: UUID("45206b70-146b-4947-9de2-aaab99c2223b").'`

      Appending `?uuidRepresentation=Standard` to the connection string and the upsert will work as expected (previous behaviour); as would setting `BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;` both of these configurations correctly store the ID as UUID() in mongo.

      Swapping BSONID for [BsonRepresentation(BsonType.Binary)] will allow the code to run otherwise unaltered but then stores the ID as BinData(3, "a1A8tGgqy0myU3jvfIW3wQ==")

      Changing the ID field to [BsonId, BsonGuidRepresentation(GuidRepresentation.Standard)]
      results in the immutable id error again.

      using MongoDB.Driver;
      using System;
      using System.Threading.Tasks;
      using MongoDB.Bson;
      using MongoDB.Bson.Serialization.Attributes;
      namespace MongoGuidStandardIssue
          public class ReadModel
              public ReadModel(Guid id)
                  Id = id;
                  CreatedDate = DateTimeOffset.UtcNow;
              public Guid Id { get; set; }
              public DateTimeOffset CreatedDate { get; set; }
          public sealed class UserReadModel : ReadModel
              public string Username { get; set; }
              public UserReadModel(Guid id) : base(id) { }
          public static class Program
              public static async Task Main(string[] args)
                  BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
                  var mongoClient = new MongoClient("mongodb://localhost:32768");
                  var readStoreDb = mongoClient.GetDatabase("test");
                  var userReadStore = readStoreDb.GetCollection<UserReadModel>(nameof(UserReadModel));
                  var userId = Guid.NewGuid();
                  var newUser = new UserReadModel(userId)
                      CreatedDate = DateTimeOffset.UtcNow,
                      Username = "Test " + DateTime.Now.Ticks.ToString()
                  var findOneAndReplaceTask = await userReadStore.FindOneAndReplaceAsync<UserReadModel>(_ => _.Id == newUser.Id, newUser, new FindOneAndReplaceOptions<UserReadModel> { IsUpsert = true, ReturnDocument = ReturnDocument.After });

      We appear to have gone from one line of code to configure ID storage to config roulette.

      Its hard to tell if this is an underlying driver issue, a lack of validation or just a communication/documentation issue; the commit messages around the change don't shine much light on it either. ( ` CSHARP-2724: Implement specification for handling native UUID types. `
       https://github.com/mongodb/mongo-csharp-driver/commit/b0b11348e1fb2418ca6a5999a2494db54ec186e5 ) the addition of new code that is immediately marked as obsolete also muddies the water a tad.



        1. image-2020-08-28-09-48-09-587.png
          80 kB
        2. MongoGuidStandardIssue.csproj
          0.3 kB
        3. Program.cs
          2 kB

        Issue Links



              Unassigned Unassigned
              chris.mckee@ivendi.com Chris McKee
              5 Vote for this issue
              9 Start watching this issue