例子:
> db.stuff.save({"foo":"bar"});
> db.stuff.find({"foo":"bar"}).count();
1
> db.stuff.find({"foo":"BAR"}).count();
0
例子:
> db.stuff.save({"foo":"bar"});
> db.stuff.find({"foo":"bar"}).count();
1
> db.stuff.find({"foo":"BAR"}).count();
0
当前回答
更新:
原来的答案现在已经过时了。Mongodb现在支持高级全文搜索,有很多特性。
最初的回答:
需要注意的是,使用regex的大小写不敏感/i进行搜索意味着mongodb不能通过索引进行搜索,因此针对大型数据集的查询可能需要很长时间。
即使数据集很小,效率也不是很高。cpu消耗比查询所保证的要大得多,如果您试图实现规模化,这可能会成为一个问题。
作为一种替代方法,您可以存储一个大写的副本并根据它进行搜索。例如,我有一个用户表,其中有一个混合大小写的用户名,但id是用户名的大写副本。这确保了区分大小写的复制是不可能的(同时拥有“Foo”和“Foo”是不允许的),并且我可以通过id = username. touppercase()进行搜索,以获得对用户名不区分大小写的搜索。
如果字段很大,比如消息体,复制数据可能不是一个好选择。我相信在这种情况下,使用像Apache Lucene这样的无关索引器是最好的选择。
其他回答
你可以使用正则表达式。
在你的例子中,这将是:
db.stuff.find( { foo: /^bar$/i } );
不过,我必须说,也许你可以降低(或提高)价值的过程中,而不是承担额外的成本,每次你找到它。显然,这对人名之类的东西不起作用,但可能用在像标签这样的用例上。
Mongo(当前版本2.0.0)不允许对索引字段进行不区分大小写的搜索——请参阅它们的文档。对于非索引字段,其他答案中列出的正则表达式应该是可以的。
从MongoDB 3.4开始,执行快速不区分大小写搜索的推荐方法是使用不区分大小写索引。
我亲自给其中一位创始人发了邮件,请他把这个工作做好,他做到了!自2009年以来,JIRA上就有这个问题,许多人都要求提供这个功能。下面是它的工作原理:
通过指定强度为1或2的排序规则,可以创建不区分大小写的索引。你可以像这样创建一个不区分大小写的索引:
db.cities.createIndex(
{ city: 1 },
{
collation: {
locale: 'en',
strength: 2
}
}
);
你也可以在创建集合时指定一个默认的排序规则:
db.createCollection('cities', { collation: { locale: 'en', strength: 2 } } );
在这两种情况下,为了使用不区分大小写的索引,你需要在find操作中指定与创建索引或集合时使用的相同的排序规则:
db.cities.find(
{ city: 'new york' }
).collation(
{ locale: 'en', strength: 2 }
);
这将返回"New York", "New York", "New York"等。
其他的笔记
The answers suggesting to use full-text search are wrong in this case (and potentially dangerous). The question was about making a case-insensitive query, e.g. username: 'bill' matching BILL or Bill, not a full-text search query, which would also match stemmed words of bill, such as Bills, billed etc. The answers suggesting to use regular expressions are slow, because even with indexes, the documentation states: "Case insensitive regular expression queries generally cannot use indexes effectively. The $regex implementation is not collation-aware and is unable to utilize case-insensitive indexes." $regex answers also run the risk of user input injection.
博士TL;
正确的方法做到这一点在mongo
不使用RegExp
使用mongodb的内置索引,搜索
第一步:
db.articles.insert(
[
{ _id: 1, subject: "coffee", author: "xyz", views: 50 },
{ _id: 2, subject: "Coffee Shopping", author: "efg", views: 5 },
{ _id: 3, subject: "Baking a cake", author: "abc", views: 90 },
{ _id: 4, subject: "baking", author: "xyz", views: 100 },
{ _id: 5, subject: "Café Con Leche", author: "abc", views: 200 },
{ _id: 6, subject: "Сырники", author: "jkl", views: 80 },
{ _id: 7, subject: "coffee and cream", author: "efg", views: 10 },
{ _id: 8, subject: "Cafe con Leche", author: "xyz", views: 10 }
]
)
第二步:
需要在你想要搜索的任何TEXT字段上创建索引,没有索引查询将会非常慢
db.articles.createIndex( { subject: "text" } )
第三步:
db.articles.find( { $text: { $search: "coffee",$caseSensitive :true } } ) //FOR SENSITIVITY
db.articles.find( { $text: { $search: "coffee",$caseSensitive :false } } ) //FOR INSENSITIVITY
这些已经用于字符串搜索进行了测试
{'_id': /.*CM.*/} ||find _id where _id contains ->CM
{'_id': /^CM/} ||find _id where _id starts ->CM
{'_id': /CM$/} ||find _id where _id ends ->CM
{'_id': /.*UcM075237.*/i} ||find _id where _id contains ->UcM075237, ignore upper/lower case
{'_id': /^UcM075237/i} ||find _id where _id starts ->UcM075237, ignore upper/lower case
{'_id': /UcM075237$/i} ||find _id where _id ends ->UcM075237, ignore upper/lower case