手动为 MongoDB 查询提供参数以支持整理功能(对于不区分大小写的索引)

时间:2023-01-05
本文介绍了手动为 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 查询提供参数以支持整理功能(对于不区分大小写的索引)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:在 C# 中使用 BsonRepresentation(BsonType.ObjectId) 与 BsonId 与 Ob 下一篇:.net中mongodb全文搜索

相关文章

最新文章