Builders<T>.Filter.Ne("fieldname", BsonNull.Value) not working

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Major - P3
    • None
    • Affects Version/s: 2.28.0
    • Component/s: None
    • None
    • None
    • Dotnet Drivers
    • 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

      Summary

      I'm using Builders<T>.Filter.Ne("fieldName", BsonNull.Value) wishing to filter objects where 'fieldName' is null. When I use this filter

      col.AsQueryable().Where(x => managerIdEmptyFilter.Inject()).ToList();

      I'm getting a TargetInvocationException, with an Inner Exception message

      _

      {"'BsonNull' is not a valid 24 digit hex string."}

      ._

      So it seems for some reason, the driver tries to convert 

      BsonNull.Value

       to an ObjectId.

      I even think I know why - the field ManagerId is a reference and since I can't use MongoDb dependencies on my data model, I'm using a ClassMap and declare the ManagerId string to be serialized as an ObjectId:

      cm.MapProperty(x => x.ManagerId).SetSerializer(new StringSerializer(BsonType.ObjectId));

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      I'm using the C# driver 2.28 on an M0 dev cluster.

      How to Reproduce

      I've created a ready-to-run repro sample that I published here: ssteiner/MongoDbSearch (github.com).

      Just put your connection string into MongoDbSearchTester.cs's 

      connectionString

       variable, and run it.

      You'll get the exception when running this line:

      var contactsWithEmptyManagerFromDb = col.AsQueryable().Where(x => managerIdEmptyFilter.Inject()).ToList();

      Additional Background

      I'm writing a generic data access layer that works on any of my entities (I have about 80 of them). and I have a generic search in my UI, where users can search any field on any entity.. so the search API takes a field by name, hence I cannot use expression syntax

      var managerIdEmptyFilter = Builders<PhoneBookContact>.Filter.Ne(x => x.ManagerId, null); 

      And if I don't store ManagerId as an ObjectId, then the search goes through, but it returns all contacts, except returning just 3 of them (contact1 has a non empty ManagerId.. so it should be filtered out)

            Assignee:
            Robert Stam
            Reporter:
            Stephan Steiner
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: