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

微笑、不失礼 提交于 2019-12-10 15:56:02

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/39145020/manually-supplying-arguments-to-a-mongodb-query-to-support-collation-feature-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!