Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually supplying arguments to a MongoDB query to support collation feature (for case insensitive index)

I have installed the current development version 3.3.11 in order to test the case insensitive index that is apparently supported according to https://jira.mongodb.org/browse/SERVER-90. I have tried this from a mongo shell and a simple test database and it does seem to work.

Unfortunately, even though one specifies collation (and strength) during index creation, one must also specify the same collation params with .find in order to get case insensitive matches. If collation is omitted from the query, index behaves in a case sensitive fashion.

Even the newest C# MongoDB driver (2.3.0-beta1) does not seem to support supplying collation params to a query. So even though I have upgraded the engine and database, C# driver, created the index with required collation, I cannot seem to get the results using the current driver.

Is there a "manual" way of supplying extra arguments to a query?

like image 267
wpfwannabe Avatar asked Aug 25 '16 12:08

wpfwannabe


1 Answers

This is now possible in the newer version of the C# mongo driver (since 2.4.0).

For example, to query against a case-insensitive index:

IMongoCollection<SomeObject> someCollection;
var results = someCollection.Find<SomeObject>(x => x.name == someName,
  new FindOptions() {  Collation = new Collation("en", strength: CollationStrength.Secondary) } )

Notice that to enjoy the power of the index, you need to specify in the query the exact same collation parameter as specified when creating the index.

like image 98
Y.L Avatar answered Sep 19 '22 23:09

Y.L