【发布时间】:2016-11-07 01:02:17
【问题描述】:
我有一个这样的文档结构:
{
"_id": "106.xxx.xxx.xxx",
"maxAge": 48,
"origin": "some_origin",
"time": "2016-07-04 11:41:47"
}
_id 包含一个我想用 pymongo 的 find_one 函数获得的 IP 地址。 我这样称呼它:
return (self.posts.find_one({"_id": ip}))
它返回的只是“none”,因为它没有找到文档。有什么想法吗?
编辑: 我也试着这样称呼它:
return (self.posts.find_one({"_id": str(ip)}))
更多信息: 我在 docker 容器中运行了一个名为“vpn_service”的数据库。 该集合也被命名为“vpn_service”。
首先我尝试像这样初始化 Mongoclient:
def __init__(self, host, port):
self.client = MongoClient(host=host, port=port)
self.db = self.client.vpn_service
self.posts = self.db.posts
我也试过这个:
def __init__(self, host, port):
self.client = MongoClient(host=host, port=port)
self.db = self.client.vpn_service
self.collection = self.db.vpn_service
self.posts = self.collection.posts
由于只是 posts.find_one() 没有找到任何东西,因此可能是连接有问题。
【问题讨论】:
-
这是您的 2 个版本的连接代码的结果: > Collection(Database(MongoClient('localhost', 27017), u'vpn_service'), u'posts') > Collection(Database( MongoClient('localhost', 27017), u'vpn_service'), u'vpn_service.posts') 这意味着第二个是错误的,因为您查询的是 'vpn_service' 数据库中的 'vpn_service.posts' 集合,而不是 'posts ' 集合
标签: python mongodb pymongo bottle