[CSHARP-1906] Case insensitive search using $in clause in Linq expressions Created: 01/Feb/17  Updated: 07/Jul/22  Resolved: 07/Jul/22

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

Type: Improvement Priority: Minor - P4
Reporter: Mikhail Belov Assignee: Robert Stam
Resolution: Duplicate Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-1950 Allow string or regular expressions i... Closed
Epic Link: CSHARP-3615

 Description   

Hi guys,

It would be very useful to support case insensitive search using $in clause in Linq expressions. For example it might be done in this way:

var goodValues = new string[] { ... }
var query = collection.AsQueryable<T>()
    .Where(i => goodValues.Contains(i.SomeField.ToLower()));

Right now it's only possible to specify general values in the array and is not possible to specify regex values for case insensitive search. General equal clause suports it via ToLower() etc methods, but the driver throw an exception if there is a call of .ToLower() method inside the array.Contains() method in .Where() expression.

Thanks,
Mikhail



 Comments   
Comment by James Kovacs [ 07/Jul/22 ]

This issue has been fixed in the new LINQ provider (known as LINQ3), which was introduced in the 2.14 release.

Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality.

To configure a client to use the LINQ3 provider use code like the following

var connectionString = "mongodb://localhost";
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.LinqProvider = LinqProvider.V3;
var client = new MongoClient(clientSettings);

Comment by Githook User [ 07/Jul/22 ]

Author:

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

Message: CSHARP-1906: Test that case insensitive searches work in LINQ. (#840)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/d0b8de5268e8b7c47bde80ea94716bf8e5667c80

Comment by Lucas Picchi [ 08/Sep/21 ]

Hi! Thanks for taking the time to explain the situation!

Comment by James Kovacs [ 07/Sep/21 ]

Hi, Lucas,

Thank you for reaching out to MongoDB. We apologize for allowing this issue and pull request to languish in our backlog for so long.

We are currently working on a new LINQ provider, which is nearing completion and includes a fix for this issue. We plan to release the new LINQ provider as an opt-in option in the next release of the driver. We will update this issue with additional details once the new provider is available.

In the meantime, a more performant solution to this problem - which will work with current and future drivers - is to define an case-insensitive index on the field and query using that collation. When defining your aggregation in C#, you would need to opt into the case-insensitive collation for that field. You can find an example of using a collation in our documentation.

Sincerely,
James

Comment by Lucas Picchi [ 06/Sep/21 ]

In 2021 this is still a problem, what is need to be done to get this merged? A lot of people in forums is asking about this feature. I would be glad to help fixing any code or conflict.

Comment by Donald Urquhart [ 18/Aug/20 ]

There was a pull request for this 3 years ago? Really need this feature.

Comment by Mikhail Belov [ 01/Feb/17 ]

I've sent pull request with this improvement:

https://github.com/mongodb/mongo-csharp-driver/pull/268

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