MongoDB的和C#:不区分大小写 [英] MongoDB and C#: Case insensitive search

查看:961
本文介绍了MongoDB的和C#:不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MongoDB的和的 MongoDB的 C#驱动程序。

我最近发现,在MongoDB中的所有查询是区分大小写的。我怎样才能做一个区分大小写的搜索?

我发现这样做的一种方式:

  Query.Matches(
    名字,
    BsonRegularEx pression.Create(新正则表达式(searchKey,RegexOptions.IgnoreCase)));


解决方案

您可能不得不重新存储领域的两倍,其真正的价值一次,并在全部小写。然后,您可以查询不区分大小写的搜索小写的版本(不要忘了还小写查询字符串)。

此方法适用(或需要)对许多数据库系统,并且它应该执行比普通前pression基础的技术(至少为preFIX或精确匹配)更好。

I am using MongoDB and the C# driver for MongoDB.

I recently discovered that all queries in MongoDB are case-sensitive. How can I make a case-insensitive search?

I found one way to do this:

Query.Matches(
    "FirstName", 
    BsonRegularExpression.Create(new Regex(searchKey,RegexOptions.IgnoreCase)));

解决方案

You will probably have to store the field twice, once with its real value, and again in all lowercase. You can then query the lowercased version for case-insensitive search (don't forget to also lowercase the query string).

This approach works (or is necessary) for many database systems, and it should perform better than regular expression based techniques (at least for prefix or exact matching).

这篇关于MongoDB的和C#:不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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