[CSHARP-4457] LINQ3 provider compiles queries on boolean fields as {$expr:field}/{$nor:[{$expr:field}]} Created: 20/Dec/22  Updated: 28/Oct/23  Resolved: 05/Jan/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.19.0

Type: Bug Priority: Major - P3
Reporter: Alkis Tsapanidis Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: linq
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File MongoBools-2.csproj     Text File MongoBools-2.sln     Text File MongoDBLinqV3BoolFieldRepro-2.cs    

 Description   

Summary

The LINQ3 provider compiles queries on boolean fields as {$expr: "$field"}, rather than {<field>:<value>}, or at the very least $eq/$neq. Worse still, it compiles negative queries on boolean fields as {$nor:[{$expr: "$field"}]}.

This stops these queries from using indexes and rewriting them all to use Builders<T> is annoying boilerplate, as the queries are obviously more complex than that on our production code.

We migrated to the LINQ V3 provider because it can handle some queries that V2 could not compile, but I cannot remember specifically which queries in our codebase those were. This has had quite a negative impact on our query targetting, I have been rewriting our queries to a verbose combo of Builder<T> and strings.

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

This has been reproduced on 2.17.1 and 2.18.0, on .NET Core 3.1 and .NET 6.0.

How to Reproduce

I have attached a small repro project that illustrates the issue, and pasted the example outputs here as well:

 

// Produces {"BoolField":true}
var findFieldV2 = testV2.Find(x => x.BoolField).ToString();
 
// Produces {"BoolProperty":true}
var findPropertyV2 = testV2.Find(x => x.BoolProperty).ToString();
 
// Produces {"BoolField":true}
var findFieldBuilderV2 = testV2.Find(Builders<TestClass>.Filter.Eq(x => x.BoolField, true)).ToString();
 
// Produces {"BoolProperty":true}
var findPropertyBuilderV2 = testV2.Find(Builders<TestClass>.Filter.Eq(x => x.BoolProperty, true)).ToString();
 
// Produces {"$expr":"$BoolField"}
var findFieldV3 = testV3.Find(x => x.BoolField).ToString();
 
// Produces {"BoolProperty":true}
var findPropertyV3 = testV3.Find(x => x.BoolProperty).ToString();
 
// Produces {"BoolField":true}
var findFieldBuilderV3 = testV3.Find(Builders<TestClass>.Filter.Eq(x => x.BoolField, true)).ToString();
 
// Produces {"BoolProperty":true}
var findPropertyBuilderV3 = testV3.Find(Builders<TestClass>.Filter.Eq(x => x.BoolProperty, true)).ToString();
 
// Produces {"BoolField":{"$ne":true}}
var negFindFieldV2 = testV2.Find(x => !x.BoolField).ToString();
 
// Produces {"BoolProperty":{"$ne":true}}
var negFindPropertyV2 = testV2.Find(x => !x.BoolProperty).ToString();
 
// Produces {"BoolField":false}
var negFindFieldBuilderV2 = testV2.Find(Builders<TestClass>.Filter.Eq(x => x.BoolField, false)).ToString();
 
// Produces {"BoolProperty":false}
var negFindPropertyBuilderV2 = testV2.Find(Builders<TestClass>.Filter.Eq(x => x.BoolProperty, false)).ToString();
 
// Produces {"$nor":[{"$expr":"$BoolField"}]}
var negFindFieldV3 = testV3.Find(x => !x.BoolField).ToString();
 
// Produces {"BoolProperty":{"$ne":true}}
var negFindPropertyV3 = testV3.Find(x => !x.BoolProperty).ToString();
 
// Produces {"BoolField":false}
var negFindFieldBuilderV3 = testV3.Find(Builders<TestClass>.Filter.Eq(x => x.BoolField, false)).ToString();
 
// Produces {"BoolProperty":false}
var negFindPropertyBuilderV3 = testV3.Find(Builders<TestClass>.Filter.Eq(x => x.BoolProperty, false)).ToString();

 



 Comments   
Comment by Githook User [ 05/Jan/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4457: LINQ3 should support filters with bool fields as well as properties.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/1330dc5029b9c1b4025c385ef728e563cd6f63ad

Comment by Alkis Tsapanidis [ 21/Dec/22 ]

Amazing, thanks Robert!

Comment by Robert Stam [ 21/Dec/22 ]

Thanks again for reporting this. The fix will be in the next release.

Comment by Robert Stam [ 20/Dec/22 ]

Thank you for reporting this.  I will look into it.

Generated at Wed Feb 07 21:48:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.