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

BulkWrite failed on Upsert

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 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);
      

       

       

            Assignee:
            Unassigned Unassigned
            Reporter:
            siganberg Francis Marasigan
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: