regex - Mongo中不区分大小写的搜索

我在 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/

相关文章:

node.js - 如何设置 useMongoClient (Mongoose 4.11.0)?

mongodb - 同一台服务器上的多个 Mongodb 实例

c# - 将字符串转换为 MongoDB BsonDocument

mongodb - 通过键字段查找 MongoDB 集合中的所有重复文档

mongodb - 如何替换mongodb文档中的子字符串

mongodb - 在 MongoDB 中克隆一个集合

node.js - 如何在 Mongoose 中创建和使用枚举

c# - 如何在C#中获取连接字符串中指定的Mongo数据库

mongodb - 对 mongodb 中的大量记录进行缓慢分页

javascript - 从 mongodb id 获取时间戳