Setting a sub-field on update throws a BsonSerializationException

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Won't Fix
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • 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?
    • None
    • None
    • None
    • None
    • None
    • None

      The following should reproduce a MongoDB.Bson.BsonSerializationException on update.

      // — Model:

      public class Test

      {     [BsonId]     public object Id \{ get; set; }

      = ObjectId.GenerateNewId();

          public string Name { get; set; }

          public byte[] Data { get; set; }

          public BsonDocument Doc { get; set; }

          public static IMongoCollection<Test> GetCollection()
         

      {         // Get DB         var db = TestLibrary.Db.MongoDb;         return db.GetCollection<Test>("Tests");     }

      }

      // — Code:

      var t = new Test();

      t.Name = "TestName";

      t.Doc = new BsonDocument();

      t.Doc["Name"] = "TestDocName";

      await c.InsertOneAsync(t, cancellationToken: cancellationToken);

      string? nullString = null;

      await c.UpdateOneAsync(
          d => d.Id == t.Id,
          Builders<Test>.Update
              .Set(d => d.Data, null)
              .Set("Name", nullString)
              .Set("Doc.Name", nullString),      // <-- Source of error
          cancellationToken: cancellationToken);

      MongoDB.Bson.BsonSerializationException: 'C# null values of type 'BsonValue' cannot be serialized using a serializer of type 'BsonValueSerializer'.'

            Assignee:
            Oleksandr Poliakov
            Reporter:
            Alex Piskarev
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: