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

UpdateDefinition can't handle casts when combined (multiple Set)

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.18.0, 2.19.0
    • Component/s: LINQ3
    • Labels:
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Summary

      UpdateDefinition can't handle casts when combined (calling multiple Set).

      Driver version: 2.19.0

      Repro

       

      using System.Linq.Expressions;
      using MongoDB.Driver;
      using MongoDB.Driver.Linq;
      var mongoUrl = MongoUrl.Create("mongodb://admin:123456@localhost/management_reports?authMechanism=SCRAM-SHA-1;authSource=admin;minPoolSize=1;retryWrites=true");
      var clientSettings = MongoClientSettings.FromUrl(mongoUrl);
      clientSettings.LinqProvider = LinqProvider.V3;
      var client = new MongoClient(clientSettings);
      var db = client.GetDatabase("test");
      var carCollection = db.GetCollection<Car>("cars");
      var updateDef = GetUpdateFieldDefinition((t => t.Type, "super-fast"), (t => t.Price, 100m));
      await carCollection.UpdateManyAsync(t => t.Type == "fast", updateDef);
      Console.ReadKey();
      static UpdateDefinition<Car> GetUpdateFieldDefinition(params (Expression<Func<Car, object>> fieldSelector, object value)[] fieldsToUpdate)
      {
          UpdateDefinition<Car> updateDefinition = null;
          foreach (var (fieldSelector, value) in fieldsToUpdate)
          {
              updateDefinition = updateDefinition?.Set(fieldSelector, value) ?? Builders<Car>.Update.Set(fieldSelector, value);
          }
          return updateDefinition;
      }
      public class Car
      {
          public string Type { get; set; }
          public decimal Price { get; set; }
      }
      

       

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            alexandre.murari@flashapp.com.br Alexandre Junior
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: