Mongo 中不区分大小写的搜索
我在 Mongo 中使用不区分大小写的搜索,类似于 https://stackoverflow.com/q/5500823/1028488 。
IE。我正在使用带有选项 i 的正则表达式。但我无法将正则表达式限制为该单词,它的执行更像是 SQL 中的“Like”
,例如:如果我使用像这样的查询 {"SearchWord" : { '$regex' : 'win', $options: '-i' }}
,它显示了 win、window & 的结果。冬天。我如何限制它只显示胜利?
我尝试了 /^win$/
但它说无效的 JSON...请建议离开。
I am using a case insensitive search in Mongo, something similar to https://stackoverflow.com/q/5500823/1028488.
ie. I am using a regex with options i. But I am having trouble restricting the regex to just that word, it performs more like a 'Like' in SQL
eg: if I use query like{"SearchWord" : { '$regex' : 'win', $options: '-i' }}
, it shows me results for win, window & winter. How do i restrict it to jsut show win?
I tried /^win$/
but it's saying invalid JSON... Please suggest away.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
$options =>; i
用于不区分大小写的搜索。给出字符串匹配所需的一些可能的示例。不区分大小写
string
包含
string
以
string
开头以
string
结尾不包含
string
代码>将其保留为书签,以及您可能需要的任何其他更改的参考。
, '$options' : 'i'}})http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/
将其保留为书签,以及您可能需要的任何其他更改的参考。
, '$options' : 'i'}})http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/
包含
string
以
string
开头以
string
结尾不包含
string
代码>将其保留为书签,以及您可能需要的任何其他更改的参考。
, '$options' : 'i'}})http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/
不包含
string
代码>将其保留为书签,以及您可能需要的任何其他更改的参考。
, '$options' : 'i'}})http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/
包含
string
以
string
开头以
string
结尾不包含
string
代码>将其保留为书签,以及您可能需要的任何其他更改的参考。
http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/
You can Use
$options => i
for case insensitive search. Giving some possible examples required for string match.Exact case insensitive
string
Contains
string
Start with
string
End with
string
Doesn't Contains
string
Keep this as a bookmark, and a reference for any other alterations you may need.
, '$options' : 'i'}})http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
Keep this as a bookmark, and a reference for any other alterations you may need.
, '$options' : 'i'}})http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
Contains
string
Start with
string
End with
string
Doesn't Contains
string
Keep this as a bookmark, and a reference for any other alterations you may need.
, '$options' : 'i'}})http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
Doesn't Contains
string
Keep this as a bookmark, and a reference for any other alterations you may need.
, '$options' : 'i'}})http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
Contains
string
Start with
string
End with
string
Doesn't Contains
string
Keep this as a bookmark, and a reference for any other alterations you may need.
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
您可以使用
'$regex':'^win$'
或/^win$/i
(注意第二个没有引号)来源:Mongo 查询中的正则表达式
You can use
'$regex':'^win$'
or/^win$/i
(notice no quote on the second one)Source here : Regex in queries with Mongo
更新:从 MongoDB 2.4 开始,人们将使用“文本”索引和全文搜索查询来执行此操作。您可以在此处阅读有关它们的信息。如果使用最新的 MongoDB,下面的方法将是愚蠢且不必要的。
但是,如果您有 MongoDB < 2.4.0 您可以使用如下正则表达式:
但是,您的版本无法正常工作,因为使用 $regex 运算符时不需要“/”:
请注意,不区分大小写的查询不使用索引,因此创建一个小写的搜索词字段可能是有意义的,这样您就可以加快查询速度。
请访问此处了解有关正则表达式的更多信息
UPDATE: As of MongoDB 2.4 one would use a "text" index and full text search query to do this. You can read about them here. If using a recent MongoDB the approach below would be silly and unecessary.
However, if you have MongoDB < 2.4.0 you could use a regular expression like so:
However, your version wasn't working because you don't need the "/"s when using the $regex operator:
Please note that case insensitive queries do not use the index so it might make sense to make a lowercase searchword field so that you can speed that query up.
Go here for more info on RegularExpressions
使用 $strcasecmp。聚合框架是在 MongoDB 2.2 中引入的。您可以使用字符串运算符“$strcasecmp”在字符串之间进行不区分大小写的比较。它比使用正则表达式更推荐且更容易。
这是关于聚合命令运算符的官方文档: https:// /docs.mongodb.com/manual/reference/operator/aggregation/strcasecmp/#exp._S_strcasecmp 。
Use $strcasecmp. The aggregation framework was introduced in MongoDB 2.2. You can use the string operator "$strcasecmp" to make a case-insensitive comparison between strings. It's more recommended and easier than using regex.
Here's the official document on the aggregation command operator: https://docs.mongodb.com/manual/reference/operator/aggregation/strcasecmp/#exp._S_strcasecmp .
对于不区分大小写
对于区分大小写,
用户表中的搜索
名称是列名称和搜索的“Vi”文本
For Case insensitive
For Case sensitive
search in user table
name is column name and "Vi" text that are searched