MongoDB:是否可以进行不区分大小写的查询? [英] MongoDB: Is it possible to make a case-insensitive query?

查看:88
本文介绍了MongoDB:是否可以进行不区分大小写的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

> db.stuff.save({"foo":"bar"});

> db.stuff.find({"foo":"bar"}).count();
1
> db.stuff.find({"foo":"BAR"}).count();
0

推荐答案

您可以使用正则表达式.

在您的示例中,将是:

db.stuff.find( { foo: /^bar$/i } );

不过,我必须说,也许您可​​以在输入过程中小写(或大写)该值,而不是每次找到它都会产生额外的费用.显然,这不适用于人们的名字之类的东西,但可能适用于诸如标记之类的用例.

I must say, though, maybe you could just downcase (or upcase) the value on the way in rather than incurring the extra cost every time you find it. Obviously this wont work for people's names and such, but maybe use-cases like tags.

这篇关于MongoDB:是否可以进行不区分大小写的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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