Mongo 中不区分大小写的搜索

发布于 2024-12-17 16:42:22 字数 391 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风吹雪碎 2024-12-24 16:42:22

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

不区分大小写 string

db.collection.find({name:{'$regex' : '^string

包含 string

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

string 开头

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

string 结尾

db.collection.find({name:{'$regex' : 'string

不包含 string代码>

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

将其保留为书签,以及您可能需要的任何其他更改的参考。
http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/

, '$options' : 'i'}})

包含 string


string 开头


string 结尾


不包含 string代码>


将其保留为书签,以及您可能需要的任何其他更改的参考。
http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/

, '$options' : 'i'}})

不包含 string代码>


将其保留为书签,以及您可能需要的任何其他更改的参考。
http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/

, '$options' : 'i'}})

包含 string

string 开头

string 结尾

不包含 string代码>

将其保留为书签,以及您可能需要的任何其他更改的参考。
http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/

, '$options' : 'i'}})

将其保留为书签,以及您可能需要的任何其他更改的参考。
http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/

, '$options' : 'i'}})

包含 string

string 开头

string 结尾

不包含 string代码>

将其保留为书签,以及您可能需要的任何其他更改的参考。
http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/

, '$options' : 'i'}})

不包含 string代码>

将其保留为书签,以及您可能需要的任何其他更改的参考。
http://www.cheatography.com/davechild/cheat-sheets/regular-表达式/

, '$options' : 'i'}})

包含 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

db.collection.find({name:{'$regex' : '^string

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

Doesn't Contains string

db.collection.find({name:{'$regex' : '^((?!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/

, '$options' : 'i'}})

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/

, '$options' : 'i'}})

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/

, '$options' : 'i'}})

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/

, '$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/

, '$options' : 'i'}})

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/

, '$options' : 'i'}})

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/

, '$options' : 'i'}})

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/

七堇年 2024-12-24 16:42:22

您可以使用 '$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

情痴 2024-12-24 16:42:22

更新:从 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" }

请注意,不区分大小写的查询不使用索引,因此创建一个小写的搜索词字段可能是有意义的,这样您就可以加快查询速度。

请访问此处了解有关正则表达式的更多信息

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

风流物 2024-12-24 16:42:22
{
          $match: {
            $expr: {
              $and: [
                { $eq: ["$_id", "$itemId"] },
                {
                  $regexMatch: {
                    input: "$brandData.name",
                    regex: "sample",
                    options: "i",
                  },
                },
              ],
            },
          },
        },
{
          $match: {
            $expr: {
              $and: [
                { $eq: ["$_id", "$itemId"] },
                {
                  $regexMatch: {
                    input: "$brandData.name",
                    regex: "sample",
                    options: "i",
                  },
                },
              ],
            },
          },
        },
厌味 2024-12-24 16:42:22

使用 $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 .

两个我 2024-12-24 16:42:22

对于不区分大小写

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

对于区分大小写,

db.users.find({"name":"Vi"})
// or
db.users.find({"email":"[email protected]"})

用户表中的搜索

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

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":"[email protected]"})

search in user table

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文