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

Querying for BsonObjectId == null causes errors

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.9.1
    • Affects Version/s: 1.8.3
    • Component/s: Serialization
    • Labels:
      None

      We are trying to upgrade from version 1.7.1 of the driver to 1.8.3. One of the problems we have run into is that BsonObjectId can no longer be saved as null in the database, instead it's saved as

      { _csharpnull : true }

      .

      I don't understand why this change was made. BsonObjectId is a class, and can be null, so why shouldn't it be represented as null in the database?

      One example of things that break now is:

      public class TestClass
      {
      public BsonObjectId SomeId

      { get; set; }

      }

      [Test]
      public void TestNullBsonObjectId()
      {

      _collection.Insert(new TestClass

      { SomeId = null }

      );

      //Breaks completely because BsonObjectId null can't be serialized
      var objects = _collection.AsQueryable<TestClass>().Where(doc => doc.SomeId == null).ToList();
      }

      So the driver is perfectly happy to save documents with a BsonObjectId property that is null to the database, but if you try to query them again to get the ones where the id is null that breaks completely. I could create a query to explicitly check for SomeId._csharpnull = True, but that just seems weirdly csharp specific.

      The LINQ query thing above is clearly a bug, although I don't see how it can be fixed. But my main question is, what would you recommend? We have millions of records that can have null values in fields of type BsonObjectId. Should we change them to a Nullable<ObjectId> instead in our CSharp code? Or something else? Create our own Id type that is not a BsonValue?

      I tried to force the 1.8 driver to behave like 1.7.1 but since it's not possible to register a custom serializer for BsonObjectId, and the serialization writes the {_csharpnull:true} thing before checking the member map I wasn't able to change it back.

      Thanks in advance,
      Einar

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            eeg@trustpilot.com Einar Egilsson
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: