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

Setting a sub-field on update throws a BsonSerializationException

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: Unknown Unknown
    • None
    • 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?

    Description

      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'.'

      Attachments

        Activity

          People

            oleksandr.poliakov@mongodb.com Oleksandr Poliakov
            alex.covecube Alex Piskarev
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: