【发布时间】:2012-09-25 12:20:37
【问题描述】:
我正在尝试使用 Python 2.7 和 pymongo-2.3 查询 MongoDB 数据库,使用如下:
from pymongo import Connection
connection = Connection()
db = connection['db-name']
collections = db.subName
entries = collections['collection-name']
print entries
# > Collection(Database(Connection('localhost', 27017), u'db-name'), u'subName.collection-name')
for entry in entries.find():
pass
即使我没有对 entry 对象做任何事情,迭代器也会失败:
Traceback (most recent call last):
File "/Users/../mongo.py", line 27, in <module>
for entry in entries.find():
File "/Library/Python/2.7/site-packages/pymongo-2.3-py2.7-macosx-10.8-intel.egg/pymongo/cursor.py", line 778, in next
File "/Library/Python/2.7/site-packages/pymongo-2.3-py2.7-macosx-10.8-intel.egg/pymongo/cursor.py", line 742, in _refresh
File "/Library/Python/2.7/site-packages/pymongo-2.3-py2.7-macosx-10.8-intel.egg/pymongo/cursor.py", line 686, in __send_message
File "/Library/Python/2.7/site-packages/pymongo-2.3-py2.7-macosx-10.8-intel.egg/pymongo/helpers.py", line 111, in _unpack_response
UnicodeDecodeError: 'utf8' codec can't decode byte 0xfc in position 744: invalid start byte
我不是我要查询的数据库的创建者。 有人知道我做错了什么以及如何解决吗?谢谢。
更新:我设法使用try-except 跳过了来自pymongo/helpers.py 的违规行,但我更喜欢不涉及数据丢失的解决方案。
try:
result["data"] = bson.decode_all(response[20:], as_class, tz_aware, uuid_subtype)
except:
result["data"] = []
【问题讨论】:
标签: python mongodb python-2.7 pymongo