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

MongoDB BsonDocument Down-Casting

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 1.6
    • None

    Description

      I have a collection in MongoDB which I'm trying to "FindAndModify" using C# driver. This collection holds types of a base class and its derived classed, as follows:

      [BsonDiscriminator(RootClass = true)]
      public class Father
      {
      public Guid Id

      { get; private set; }
      }

      public class Son : Father
      {
      public string SomeProperty { get; private set; }

      }

      When I'm trying to cast the BsonDocument to my base class, after the FindAndModify, where the result is "Son":

      Father modifiedDocument = result.GetModifiedDocumentAs<Father>();

      I get the following exception:

      System.IO.FileFormatException: Element 'SomeProperty' does not match any field or property of class Father.

      Any idea why? -Can't I perform a down cast here?

      Thanks,
      Nir.

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            nirpi Nir Pinhasov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: