-
Type: Bug
-
Resolution: Won't Fix
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.11.0
-
Component/s: API, BSON, Documentation
-
None
-
Environment:All
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 { [Serializable] public class ReadModel { public ReadModel(Guid id) { Id = id; CreatedDate = DateTimeOffset.UtcNow; } [BsonId] public Guid Id { get; set; } [BsonRepresentation(BsonType.String)] 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 }); Console.WriteLine("Done"); Console.ReadLine(); } } }
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.
Thanks
- depends on
-
CSHARP-2930 Change default GuidRepresentationMode to V3 and default GuidRepresentation to Unspecified
- Closed
- is related to
-
CSHARP-3179 FilterDefinition don't respect GuidRepresentation
- Backlog