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

Update "set" operator ignores [BsonIgnoreIfNull] attribute

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.2.4
    • Component/s: Builders, Serialization
    • Labels:
      None

      Update set operator ignores [BsonIgnoreIfNull] attribute and sets value to null. Example code:

              public class Entity
              {
                  public int Id { get; set; }
      
                  [BsonIgnoreIfNull]
                  public string S { get; set; }
              }
      
              [Test]
              public void Test()
              {
                  var collection = new MongoClient().GetDatabase("Test").GetCollection<Entity>("Test");
                  collection.UpdateOne(x => x.Id == 1, Builders<Entity>.Update.Set(x => x.S, null), new UpdateOptions { IsUpsert = true });
              }
      

      The result JSON in MongoDB is:

      { 
          "_id" : NumberInt(1), 
          "S" : null
      }
      

      Expected:

      { 
          "_id" : NumberInt(1)
      }
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            eshchelkanov Eugeny Shchelkanov
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: