我在 Mongo 中使用不区分大小写的搜索,类似于 https://stackoverflow.com/q/5500823/1028488 .
即我正在使用带有选项 i 的正则表达式。但我无法将正则表达式限制为那个词,它的执行更像是 SQL 中的“喜欢”
例如:如果我使用类似的查询
{"SearchWord": { '$regex' : 'win', $options: '-i' }}
,它显示了 win、window 和 winter 的结果。我如何将其限制为 jsut show win?
我试过 /^win$/
但它说 JSON 无效...请建议离开。
提前致谢
最佳答案
您可以使用 $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'}})
将此作为书签保存,并作为您可能需要的任何其他更改的引用。 http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
https://stackoverflow.com/questions/8246019/