[英]using regex in pymongo with variable value
i have a variable name
in mongo shell i can query like this我在 mongo shell 中有一个变量
name
,我可以这样查询
db.xxxx.find({"path" : {"$regex" : name , "$options" : "i"}})
but i use in in pymongo like this但我像这样在 pymongo 中使用
query ={"path" : {"$regex" : name , "$options" : "i"}}
result=list(db.xxxx.find(query))
it return [] .它返回 [] 。 when i check query i got that there is a problem query in like this imagine
name==hyper
当我检查查询时,我发现有一个问题查询,就像这样想象
name==hyper
query = {"path" : {"$regex" : '"hyper"', "$options" : "i"}}
there is " in name that cause empty result how can fix this ?有“在名称中导致空结果如何解决这个问题?
This example seems to work;这个例子似乎有效; see if it helps you:
看看它是否对你有帮助:
import pymongo
db = pymongo.MongoClient()['mydatabase']
db.mycollection.delete_many({})
db.mycollection.insert_one({'path': 'You Now Have \"Two Problems\"'})
name = 've \"tw'
query ={"path" : {"$regex" : name , "$options" : "i"}}
print(list(db.mycollection.find(query)))
Result:结果:
[{'_id': ObjectId('5e18c97d2b0b356dbd0019a1'), 'path': 'You Now Have "Two Problems"'}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.