i have a variable name
in mongo shell i can query like this
db.xxxx.find({"path" : {"$regex" : name , "$options" : "i"}})
but i use in in pymongo like this
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
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"'}]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.