[CSHARP-4115] Query against strings (case insensitive) Created: 28/Mar/22  Updated: 18/Jan/24

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

Type: Bug Priority: Unknown
Reporter: Theodore Spence Assignee: Robert Stam
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-4100 Review uses of StringComparison in St... Closed

 Description   

I am using the MongoDB C# client to construct queries like "Name > 'Aardvark'".  I can easily create a query that filters on strings case-sensitively, like this:

return Builders<T>.Filter.Gt("Name", "Aardvark");

I can't seem to find an option for comparing strings case insensitively.  If I was writing the query directly in MongoDB, the query would look like this:

{ $expr: { $gte: [ {{{ $strcasecmp: [ "$Name", "}}Aardvark"] }, }

It seems like it should be possible to do this with the .Where() function, but that function only recognizes `obj => obj.Name.Equals("Aardvark", OrdinalIgnoreCase)`.  It doesn't seem to permit any comparisons.

When I try the following, I get an error executing my query:

return Builders<T>.Filter.Where(x => String.Compare(x.Name, "Aardvark", OrdinalIgnoreCase);

Is there a better way to do this, or is this not implemented today?



 Comments   
Comment by Theodore Spence [ 29/Mar/22 ]

If it helps: My use case doesn't require precise collation logic. As long as there's a way to address the challenges of sorting in languages that have different capital characters and lowercase characters yet treat them somewhat interchangeably, I will be happy enough with the solution.

I would also be totally okay if the driver just had a `Builder.StrCaseCmp(field, value, comparison)` function and you had to intentionally choose to use that so there was no ambiguity about which collation rules would be in effect.

Comment by Theodore Spence [ 29/Mar/22 ]

Awesome, thank you so much for the update!  I think I didn't see that Jira ticket originally because I was searching for `$strcasecmp`.

I submitted https://github.com/mongodb/mongo-csharp-driver/pull/758 as a rudimentary way of executing this, but please feel free to reject the PR or make use of it however you prefer.

Comment by Robert Stam [ 29/Mar/22 ]

We are working on this as part of CSHARP-4100.

Note that you will use `StringCompare.CurrentCultureIgnoreCase` instead of `StringCompare.OrdinalIgnoreCase`.

This is because the server does not have much support for ordinal operations. What it does have is the ability to set a "collation" for the entire pipeline. We have decided that the best we can do is interpret "current culture" as a way of saying "pipeline collation".

 

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