【问题标题】:Case insensitive search in MongoMongo中不区分大小写的搜索
【发布时间】:2012-01-04 23:18:25
【问题描述】:

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

即我正在使用带有选项 i 的正则表达式。但我无法将正则表达式限制为那个词,它的执行更像是 SQL 中的“喜欢”

例如:如果我使用类似的查询 {"SearchWord" : { '$regex' : 'win', $options: '-i' }},它显示了胜利、窗口和冬季的结果。我如何将其限制为 jsut show win?

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

提前致谢

【问题讨论】:

    标签: regex mongodb pattern-matching match case-insensitive


    【解决方案1】:

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

    不区分大小写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', '$options' : 'i'}})
    

    string结尾

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

    不包含string

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

    将此作为书签保存,并作为您可能需要的任何其他更改的参考。 http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

    【讨论】:

    • 错误地,您将“包含字符串”的示例放在“不区分大小写的字符串”中,反之亦然。我已通过交换这两种情况的示例来编辑您的答案,并纠正了一些语法错误。
    • @gauravparmar:如有不妥,请检查并更正。
    • 我已经编辑了你的答案,但它必须经过同行评审,它说。
    【解决方案2】:

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

    来源:Regex in queries with Mongo

    【讨论】:

      【解决方案3】:

      更新:从 MongoDB 2.4 开始,可以使用“文本”索引和全文搜索查询来执行此操作。您可以在here 阅读有关它们的信息。如果使用最近的 MongoDB,下面的方法将是愚蠢和不必要的。

      但是,如果您有 MongoDB

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

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

      转到here 以获取有关正则表达式的更多信息

      【讨论】:

      【解决方案4】:

      不区分大小写

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

      区分大小写

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

      在用户表中搜索

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

      【讨论】:

        【解决方案5】:
        {
                  $match: {
                    $expr: {
                      $and: [
                        { $eq: ["$_id", "$$itemId"] },
                        {
                          $regexMatch: {
                            input: "$brandData.name",
                            regex: "sample",
                            options: "i",
                          },
                        },
                      ],
                    },
                  },
                },
        

        【讨论】:

          【解决方案6】:

          使用 $strcasecmp。聚合框架是在 MongoDB 2.2 中引入的。您可以使用字符串运算符“$strcasecmp”在字符串之间进行不区分大小写的比较。它比使用正则表达式更推荐和更容易。

          这里是关于聚合命令操作符的官方文档:https://docs.mongodb.com/manual/reference/operator/aggregation/strcasecmp/#exp._S_strcasecmp

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-09-15
            • 2016-02-17
            • 2013-09-26
            • 2023-03-20
            • 1970-01-01
            • 2010-10-13
            • 2011-07-20
            相关资源
            最近更新 更多