手动为 MongoDB 查询提供参数以支持整理功能(对于不区分大小写的索引)
- 作者: 单身-爱狗人士他爹
- 来源: 51数据库
- 2022-10-28
问题描述
我已经安装了当前的开发版本 3.3.11 以测试根据 http://www.51sjk.com/Upload/Articles/1/0/335/335291_20221028143809471.我已经在 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 http://www.51sjk.com/Upload/Articles/1/0/335/335291_20221028143811189.jpg 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.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
