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

BulkWrite failed on Upsert

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Works as Designed
    • Affects Version/s: 2.7.0
    • Fix Version/s: None
    • Component/s: Command Operations
    • Labels:
      None

      Description

      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);
      

       

       

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: