手动向MongoDB查询提供参数以支持排序规则功能(不区分大小写的索引) [英] Manually supplying arguments to a MongoDB query to support collation feature (for case insensitive index)

查看:79
本文介绍了手动向MongoDB查询提供参数以支持排序规则功能(不区分大小写的索引)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了当前的开发版本3.3.11,以便测试不区分大小写的索引,该索引显然根据 https://jira.mongodb.org/browse/SERVER-90 .我已经从mongo shell和简单的测试数据库中进行了尝试,并且确实似乎可行.

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.

不幸的是,即使在索引创建过程中指定了排序规则(和强度),也必须为.find指定相同的排序规则参数,以便获得不区分大小写的匹配.如果查询中省略排序规则,则索引的行为区分大小写.

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.

即使是最新的C#MongoDB驱动程序( 2.3.0-beta1 )似乎不支持向查询提供排序规则参数.因此,即使我已经升级了引擎和数据库C#驱动程序,并使用所需的排序规则创建了索引,但似乎无法使用当前驱动程序获得结果.

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?

推荐答案

现在可以在较新版本的C#mongo驱动程序(自2.4.0开始)中实现.

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.

这篇关于手动向MongoDB查询提供参数以支持排序规则功能(不区分大小写的索引)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆