【问题标题】:search a mongodb record which has "." (dot) in record name搜索具有“。”的 mongodb 记录。记录名称中的(点)
【发布时间】:2018-09-11 12:26:29
【问题描述】:

数据库中的记录:

{
    "_id" : ObjectId("5b97b20e7b473d1ee468a972"),
    "name" : "insearch.com wiki",
    "description" : "insearch",
    "createdAt" : ISODate("2018-09-11T12:16:14.181Z"),
    "updatedAt" : ISODate("2018-09-11T12:16:14.181Z"),
    "__v" : 0
}

搜索查询

db.getCollection('contents').find({name:{$regex: '.', $options: "i"}})

当我给“。”时如何搜索记录(仅点运算符)在搜索框中。

【问题讨论】:

    标签: regex mongodb-query


    【解决方案1】:

    . 句点字符在匹配任何字符的正则表达式中具有特殊含义。要禁用该行为并匹配句点本身,您需要对其进行转义:

    db.getCollection('contents').find({name:{$regex: '\\.', $options: "i"}})
    

    要以编程方式执行此操作(如您使用搜索框的情况),请参阅Is there a RegExp.escape function in Javascript?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      相关资源
      最近更新 更多