忽略MongoDB查询中的多个空白字符 [英] Ignoring Multiple Whitespace Characters in a MongoDB Query

查看:168
本文介绍了忽略MongoDB查询中的多个空白字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MongoDB查询,用于搜索地址.问题是,如果用户不小心添加了多余的空格,则查询将找不到该地址.例如,如果用户键入123 Fakeville St而不是123 Fakeville St,则查询将不返回任何结果.

I have a MongoDB query that searches for addresses. The problem is that if a user accidentally adds an extra whitespace, the query will not find the address. For example, if the user types 123 Fakeville St instead of 123 Fakeville St, the query will not return any results.

是否有一种简单的方法可以使用$regex处理此问题?我猜想在门牌号(123)和街道名称(Fakeville)之间需要忽略空格.我的查询设置如下:

Is there a simple way to deal with this issue, perhaps using $regex? I guess the space would need to be ignore between the house number (123) and the street name (Fakeville). My query is set up like this:

@app.route('/getInfo', methods=['GET'])
def getInfo():
    address = request.args.get("a")
    addressCollection = myDB["addresses"]
    addressJSON = []
    regex = "^" + address

    for address in addressCollection.find({'Address': {'$regex':regex,'$options':'i'} },{"Address":1,"_id":0}).limit(3):
        addressJSON.append({"Address":address["Address"]})
    return jsonify(addresses=addressJSON)

推荐答案

在发送查询之前先对其进行清理:

Clean up the query before sending it off:

>> import re
>>> re.sub(r'\s+', ' ', '123  abc')
'123 abc'
>>> re.sub(r'\s+', ' ', '123    abc def   ghi')
'123 abc def ghi'

您可能需要确保对数据库中的数据进行类似的标准化.对于标点符号也要考虑类似的策略.

You'll probably want to make sure that the data in your database is similarly normalised. Also consider similar strategies for things like punctuation.

实际上,为此使用正则表达式似乎过于严格,并且重新发明了轮子.考虑使用适当的搜索引擎,例如Lucene或Elasticsearch.

In fact, using a regex for this seems overly strict, as well as reinventing the wheel. Consider using a proper search engine such as Lucene or Elasticsearch.

这篇关于忽略MongoDB查询中的多个空白字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆