python - 使用 pymongo 对同一索引执行多个正则表达式匹配

标签 python mysql mongodb pymongo

我需要将一些 MySQL 转换为 mongodb。我有一个 MySQL 查询,在一列上有多个正则表达式匹配项。是否可以用 mongodb 做同样的事情?

SELECT * FROM table WHERE column1 REGEXP r1 AND column1 REGEXP r2 AND colum1 REGEXP r3;

借助 pymongo,我可以像这样使用单个正则表达式执行正则表达式搜索:

regex1 = re.compile(r1)
db.collection.find({"column1":regex1})

db.collection.find({"column1":{"$regex":"r1"}})

如何在 column1 上添加多个正则表达式?

这些不起作用:

{"column1":regex1,"column1":regex2}
{"column1":{"$regex":"r1","$regex":"r2"}}
{"column1":[regex1,regex2]}
{"column1":{"$regex":["r1","r2"]}}

最佳答案

db.collection.find({"column1": {"$all": [re.compile("r1"), re.compile("r2")]}})

也看看 future $and operator .

顺便说一句,除非你的正则表达式是前缀式的 (/^text/) 索引 won't be used .

关于python - 使用 pymongo 对同一索引执行多个正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6264263/

相关文章:

python - 如何修复单击网页按钮时的 NoSuchElementException Python 错误?

python - Pandas 数据框 | groupby 绘图 |堆叠图和并排图

mongodb - meteor $and 与 $or

javascript - 获取背景图片url值

linux - 我如何加入 MongoDB 中的字段?

python - PIP 包是否经过策划?安装它们安全吗?

python - 使用 MySQL 管理 Web 应用程序的用户身份验证

php - 调用第 46 行\sites\all\modules\community\entityreference\entityreference.install 中未定义的函数entityreference_get_behavior_handlers()

php - Mysqlnd 无法连接到 MySQL 5.5 服务器

php - 如果我只执行一个查询,那么使用事务的好处是否足够重要?