python - 使用 pymongo 查询 mongodb

标签 python mongodb python-2.7 pymongo

我刚刚开始使用 mongodb 并设置一个测试数据库来处理我创建的几个脚本的网页抓取结果。现在,date_found 正在作为字符串加载。当我在 mongohub 中运行它时:

{"date_found" : /.*2015-05-02.*/}

我得到了所有带有“2015-05-02”的集合。太棒了!

但是,当我运行时:

for item in collection.find({"date_found": "/.*2015-05-02.*/"}):
    print item

我什么也没得到。

还有,这个:

for item in collection.find():
    print item

给了我所有的集合,所以看起来一切都在我可以查询数据库的范围内。

有人可以告诉我我犯了什么愚蠢的错误(或者我错过了什么)吗?

最佳答案

在 pymongo 中,包含 regular expression 你可以尝试这样的事情:

import re
regx = re.compile(".*2015-05-02.*")
for item in collection.find({"date_found": regx})
    print item

或者使用$regex运算符:

import re
regx = re.compile(".*2015-05-02.*")
for item in collection.find({"date_found": {"$regex": regx} })
    print item

关于python - 使用 pymongo 查询 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30032949/

相关文章:

python - 如何保存 python 的 IDLE 的自定义首选项?

python - 列表的 get() 的惯用 python 等价物是什么?

mongodb - Mongo docker-具有indexCreate的Dockerfile

node.js - Mongoose 可选的搜索查询参数?

mongodb - 将 Cloudant 服务与 Bluemix 分开

python - 为什么文件夹中的所有图像没有显示在 Canvas 框架内的 tkinter 标签上?

python - 如何加速Python for循环

python - Django 表单继承不起作用 __init__

python - python 中的 Thrift TTransportException

python - 用 C 扩展 python,返回 numpy 数组给出垃圾