简体   繁体   English

PyMongo-使用变量执行SQL'LIKE'查询

[英]PyMongo - Using variables to sql 'LIKE' queries

The answer here applies when using PyMongo to search fields containing a search text. 当使用PyMongo搜索包含搜索文本的字段时, 这里的答案适用。 But only for hard-coded string values: 但仅适用于硬编码的字符串值:

 db.houses.find({"hid":{"$regex": u"9"}})

So, how to shape that expression replacing that "9" with a variable? 那么,如何调整表达式以变量替换"9"呢?

def some_func(search_text)
    db.houses.find({"hid":{"$regex": ??????}})

Use re module to create a regex for your search string 使用re模块为您的搜索字符串创建一个正则表达式

import re
search_string = re.compile('9')
db.houses.find({'hid': search_string})

you can use a lot of options available in re module 您可以使用re模块中提供的许多选项

Try this, 尝试这个,

search_text = u"9"
db.houses.find({"hid":{"$regex": search_text }})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM