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

Unable to cast object of type 'MongoDB.Bson.BsonNull' to type 'MongoDB.Bson.BsonDocument

    • Type: Icon: Task Task
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Change Streams
    • 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?

      Account/Company Name: Synergy Sports Technology LLC

      Issue + Relevant Info

      Customer is getting the following error message in their C# code after upgrading to version 7.0. They are running changeStream and their code requires pre- and post-images (have [changeStreamPreAndPostImages.enabled: true] set)

      System.InvalidCastException: Unable to cast object of type 'MongoDB.Bson.BsonNull' to type 'MongoDB.Bson.BsonDocument'.
      at MongoDB.Bson.BsonValue.get_AsBsonDocument()
      at MongoDB.Driver.ChangeStreamDocument`1.get_FullDocumentBeforeChange()
      

      They are running C# version 2.20.0 and is compatible with 7.0
      https://www.mongodb.com/docs/drivers/csharp/current/compatibility/#std-label-csharp-compatibility-tables

      Found this Jira that might be related - CSHARP-4745

      Portion of customer's code snippets they use for changeStreams:

      await database.WatchAsync(pipeline, new ChangeStreamOptions
      {
      ResumeAfter = token.Token,
      FullDocument = ChangeStreamFullDocumentOption.WhenAvailable,
      FullDocumentBeforeChange = ChangeStreamFullDocumentBeforeChangeOption.WhenAvailable
      }, cancellationToken);
      

      this gets full document from ChangeStreamDocument<BsonDocument> change like this:
      var fullDocument = change.FullDocument != default ? change.FullDocument : change.FullDocumentBeforeChange; - this line throws an exception

      Requested Action

      • Investigate and advise what may be the cause and workaround
      • Verify if this is may be related to CSHARP-4745

      Status

      Customer Name Synergy Sports Technology LLC
      Is the customer production-down NOW? *NO*
      Intercom/Case Link and Severity NA
      Link to Cluster Org/Project/Cluster Link - (M20 Instance)
      Affected Nodes & Status
      Have I attempted to restart the affected nodes? *NO*

            Assignee:
            Unassigned Unassigned
            Reporter:
            raymond.chan@mongodb.com Raymond Chan
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: