[CSHARP-4096] LINQ: Simplify multiple Where clauses in LINQ Query into a single $match clause. Created: 12/Mar/22 Updated: 14/Mar/22 Resolved: 14/Mar/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Rajesh Vinayagam | Assignee: | James Kovacs |
| Resolution: | Won't Do | Votes: | 0 |
| Labels: | LINQ | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Epic Link: | DOCSP-19501 |
| Description |
|
var query1 = collection.AsQueryable() } } }, { "$match" : { "entity.age" : { "$lt" : 100 } } }])}var query2 = collection.AsQueryable() , "$lt" : 100 } } }])} Is there a provision for Mongo C# driver to interpret the above two interpretation of similar linq statement as same mongo query i.e with a single $match instead of seperate $match for each where clauses, the obvious answer for the above problem is identify all the queries with multiple where claused to a single where clause. Other than that do we have any other suggestions. |
| Comments |
| Comment by James Kovacs [ 14/Mar/22 ] | |||||||||||||||||||||||||
|
I tried to repro the behaviour in MongoDB 4.4 and 5.0. Both exhibit the same expected behaviour that two $match stages are equivalent to a single $match stage using $and.
The query hashes (not shown) are identical indicating that the server considers them to be the same query and thus will result in identical index selection. Given that the MongoDB server's query optimizer already implements this optimization, it doesn't make sense for the .NET/C# driver to also implement the same optimization. If in your particular use case you observe suboptimal index selection, this may be more a question for the Query team why the optimization cannot be applied. |