【问题标题】:using regex in pymongo with variable value在 pymongo 中使用具有变量值的正则表达式
【发布时间】:2020-04-27 14:27:11
【问题描述】:

我在 mongo shell 中有一个变量 name 我可以这样查询

db.xxxx.find({"path" : {"$regex" : name , "$options" : "i"}})

但我像这样在pymongo中使用

query ={"path" : {"$regex" : name , "$options" : "i"}}
result=list(db.xxxx.find(query))

它返回 [] 。当我检查查询时,我发现像这样想象name==hyper

 query = {"path" : {"$regex" : '"hyper"', "$options" : "i"}}

名称中有“导致空结果如何解决?

【问题讨论】:

    标签: python-3.x pymongo pymongo-3.x


    【解决方案1】:

    这个例子似乎有效;看看对你有没有帮助:

    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)))
    

    结果:

    [{'_id': ObjectId('5e18c97d2b0b356dbd0019a1'), 'path': 'You Now Have "Two Problems"'}]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 2014-06-06
      • 2022-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多