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

Wrong conversion of Expression x => !x.value into filter if value is boolean

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.9.3
    • Component/s: API
    • None

      Model

      public class MyClass
      {   
         public ObjectId Id { get; set; }
       
         public bool MyBoolean { get; set; }
         public string Name { get; set; }
      }
      

      Filter with expression

      var filter = Collection.Find(x => !x.MyBoolean)
      // filter.ToString()
      

      converts into

      { 
         "MyBoolean": { $ne: true}
      }
      

      but should converts into

      { "MyBoolean": false }
      

      cause its boolean and it is not ignored (can't be `BsonNull`).

      It's important in cases where you want to use sort by index and had compound index with "MyBoolean" in it.
      So now if you had index like {MyBoolean: 1, Name: 1} this query can't use it:

      Collection.Find(x => !x.MyBoolean).SortBy(x => x.Name)
      

      because of filter {"MyBoolean":{$ne:true}} and you have to write it like this:

      Collection.Find(x => x.MyBoolean == false).SortBy(x => x.Name)
      

      to get filter

      { "MyBoolean": false }

      and achieve sorting by index.

            Assignee:
            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            Reporter:
            mercurochromo@gmail.com Victor Trusov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: