我有这样的文档:
{'my_key': '2019-carbeureter'}
我想搜索“2019”
db.collection.find({'my_key': query_string } # isn't working with the following
query_string = "/2019/" # doesn't work
query_string = r"/2019/" # same thing
query_string = ".*2019.*" # doesn't work
另一个SO问题在原生mongoDB中解决了这个问题,但是如何使用pymongo在Python中做到这一点呢?How to query MongoDB with "like"?
发布于 2019-04-11 00:34:54
db.collections.find({'my_key': <str>}) # Isn't going to work as a bare str
您需要定义一个对象,向该对象添加一个'$regex‘运算符/键,然后值就是熟悉的正则表达式样式:'.*2019.*'
query_string = {'$regex': '.*2019.*'}
results = db.collections.find({'my_key': query_object})
或者完全写成:
results = db.collections.find({'my_key': {'$regex': '.*2019.*'}})
https://stackoverflow.com/questions/55617412
复制相似问题