Regex Mongo中不区分大小写的搜索

Regex Mongo中不区分大小写的搜索,regex,mongodb,pattern-matching,match,case-insensitive,Regex,Mongodb,Pattern Matching,Match,Case Insensitive,我在Mongo中使用了不区分大小写的搜索,类似于 ie我使用带有选项I的正则表达式。但是我很难将正则表达式限制为这个词,它的性能更像SQL中的“like” 如果我使用像这样的查询 {“SearchWord”:{'$regex':'win',$options:'-i'},它向我显示win、window和winter的结果。如何将其限制为jsut show win 我尝试了/^win$/,但它在Json中的说法无效。。。。请建议一条路 提前感谢您可以使用'$regex':“^win$”或/^win$

我在Mongo中使用了不区分大小写的搜索,类似于

ie我使用带有选项I的正则表达式。但是我很难将正则表达式限制为这个词,它的性能更像SQL中的“like”

如果我使用像这样的查询
{“SearchWord”:{'$regex':'win',$options:'-i'}
,它向我显示win、window和winter的结果。如何将其限制为jsut show win

我尝试了
/^win$/
,但它在Json中的说法无效。。。。请建议一条路


提前感谢

您可以使用
'$regex':“^win$”
/^win$/i
(注意第二个选项上没有引号)


这里的来源:

更新:从MongoDB 2.4开始,可以使用“文本”索引和全文搜索查询来完成此操作。你可以读到他们。如果使用最近的MongoDB,下面的方法将是愚蠢和不必要的

但是,如果MongoDB<2.4.0,则可以使用如下正则表达式:

> db.reg.insert({searchword: "win"})
> db.reg.insert({searchword: "window"})
> db.reg.insert({searchword: "Win"})

> db.reg.find()
{ "_id" : ObjectId("4ecd2e33dd68c9021e453d12"), "searchword" : "win" }
{ "_id" : ObjectId("4ecd2e36dd68c9021e453d13"), "searchword" : "window" }
{ "_id" : ObjectId("4ecd2e39dd68c9021e453d14"), "searchword" : "Win" }

> db.reg.find({ searchword: /^win$/i })
{ "_id" : ObjectId("4ecd2e33dd68c9021e453d12"), "searchword" : "win" }
{ "_id" : ObjectId("4ecd2e39dd68c9021e453d14"), "searchword" : "Win" }
但是,您的版本不起作用,因为在使用$regex运算符时不需要“/”:

> db.reg.find({ searchword: { $regex: "^win$", $options: '-i' }})
{ "_id" : ObjectId("4ecd2e33dd68c9021e453d12"), "searchword" : "win" }
{ "_id" : ObjectId("4ecd2e39dd68c9021e453d14"), "searchword" : "Win" }
请注意,不区分大小写的查询不使用索引,因此创建一个小写的searchword字段可能有意义,这样可以加快查询速度

有关RegularExpressions的详细信息,请参见不区分大小写的

db.users.find({"name":{ $regex : new RegExp("Vi", "i") }})
db.users.find({"name":"Vi"})
// or
db.users.find({"email":"example@mail.com"})
区分大小写

db.users.find({"name":"Vi"})
// or
db.users.find({"email":"example@mail.com"})
在用户表中搜索


name是搜索的列名和“Vi”文本

您可以使用
$options=>i
进行不区分大小写的搜索。给出字符串匹配所需的一些可能的示例

完全不区分大小写
string

db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
包含
字符串

db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
字符串开始

db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
字符串结尾

db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
不包含
字符串

db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
将此作为书签,并作为您可能需要的任何其他修改的参考。
使用$strcasecmp。MongoDB 2.2中引入了聚合框架。您可以使用字符串运算符“$strcasecmp”在字符串之间进行不区分大小写的比较。它比使用正则表达式更容易,也更值得推荐


下面是关于聚合命令操作符的官方文档:。

$text不是regex的替代品。这是相当严格的限制,不允许他通过搜索“赢”来找到“冬天”。@BT你是对的。为了做到这一点,我们必须存储n克并匹配这些数据。另外:您错误地将“Contains string”的示例放在了“Exact-case-insensitive string”中,反之亦然。我已经编辑了你的答案,将这两个案例的例子交换了一下,还更正了一些语法错误。@gauravparmar:请检查并更正是否有任何错误。我已经编辑了你的答案,但它必须经过同行评审,它说。