-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Minor - P4
-
None
-
Affects Version/s: 2.7.0
-
Component/s: Command Operations
-
None
Bulkwrite throw duplicate exception on a collection with unique key index (non-primary index) even setting up the UpdateOneModel Upsert = true.
Ex:
// code placeholder var bulkOps = new List<WriteModel<Student>>(); var bulkOps = new List<WriteModel<Student>>(); foreach (var student in students) { var newId = Guid.NewGuid(); var update = Builders<Student>.Update .Set(a => a.FirstName, student.FirstName) .Set(a => a.LastName, student.LastName) .Set(a => a.SomeField1, student.SomeField1) .Set(a => a.LastModifiedDate, student.LastModifiedDate) .SetOnInsert(a => a.PostedDate, student.PostedDate) .SetOnInsert(a => a.Id, newId); var upsertOne = new UpdateOneModel<Student>(Builders<Student>.Filter.Where(x => x.FirstName == student.FirstName && x.LastName == student.LastName), update) { IsUpsert = true }; bulkOps.Add(upsertOne); //-- if executing individually, upsert works just fine. // var result = await Collection.UpdateOneAsync(x => x.FirstName == student.FirstName && x.LastName == student.LastName, update, new UpdateOptions { IsUpsert = true }); } //-- This will throw duplicate exceptionvar result = await Collection.BulkWriteAsync(bulkOps);