【问题标题】:Use mongodb document to match a search使用 mongodb 文档匹配搜索
【发布时间】:2014-09-26 15:30:46
【问题描述】:

我有一个 mongoDB 文档集合。我在文档中的字段是一个正则表达式。 我想从用户那里获取一个输入字符串 & 我希望 MongoDB 中的正则表达式匹配给定的字符串并回显出结果文档。 例如:

"_id": {
        "$oid": "5eeeef2a132cb8992da8"
    },
    "type": "integer",
    "regex": "\d+",
    "example": "12345"
}

如果有人现在输入一个像 776847 这样的字符串,它应该返回集合比较正则表达式和字符串并回显整数等...

有什么最好的方法吗?

提前致谢。

【问题讨论】:

    标签: php regex mongodb


    【解决方案1】:

    如果你理解你的意思是如果用户输入是:“1234”

    你的 mongo 数据库就像:

    "_id": {
            "$oid": "5eeeef2a132cb8992da8"
        },
        "type": "integer",
        "regex": "\d+",
        "example": "12345"
    }
    

    结果将是整数。

    你可以提取所有的mongo数据并循环它

    你可以用mongodb游标使用mongo find方法

    示例: 它在 python 中,但你会得到一般的想法

    def func():
       search = collection.find({'example':<user input>})
       if search:
          result = re.search(search['regex'],<user input>)
          if result != []:
             return = search['type']
          else:
             return "No match"
    

    希望对你有帮助

    B.R

    咸鱼

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-06
      • 2013-03-28
      • 2014-12-16
      • 2013-07-27
      • 2020-06-28
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      相关资源
      最近更新 更多