Mongo 中不区分大小写的搜索

我在 Mongo 中使用了一个不区分大小写的搜索,类似于 https://stackoverflow.com/q/5500823/1028488

即我使用带有选项 i 的正则表达式。但是我很难将正则表达式仅限于这个单词,它在 SQL 中的执行方式更像是“ Like”

如果我使用查询类似 {"SearchWord" : { '$regex' : 'win', $options: '-i' }},它显示了我的胜利,窗口和冬天的结果。我如何限制它只是显示胜利?

我尝试 /^win$/,但它说无效的 JSON... 请建议离开。

先谢谢你

94967 次浏览

You can use '$regex':'^win$' or /^win$/i (notice no quote on the second one)

Source here : Regex in queries with Mongo

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:

> 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" }

However, your version wasn't working because you don't need the "/"s when using the $regex operator:

> db.reg.find({ searchword: { $regex: "^win$", $options: '-i' }})
{ "_id" : ObjectId("4ecd2e33dd68c9021e453d12"), "searchword" : "win" }
{ "_id" : ObjectId("4ecd2e39dd68c9021e453d14"), "searchword" : "Win" }

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

For Case insensitive

db.users.find({"name":{ $regex : new RegExp("Vi", "i") }})

For Case sensitive

db.users.find({"name":"Vi"})
// or
db.users.find({"email":"example@mail.com"})

search in user table

name is column name and "Vi" text that are searched

You can Use $options => i for case insensitive search. Giving some possible examples required for string match.

Exact case insensitive string

db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})

Contains string

db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})

Start with string

db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})

End with string

db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})

Doesn't Contains string

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

Keep this as a bookmark, and a reference for any other alterations you may need. http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

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 .

{
$match: {
$expr: {
$and: [
{ $eq: ["$_id", "$$itemId"] },
{
$regexMatch: {
input: "$brandData.name",
regex: "sample",
options: "i",
},
},
],
},
},
},