0

我正在尝试使用 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

我不是我要查询的数据库的创建者。有人知道我做错了什么以及如何解决吗?谢谢。


更新:我设法跳过了pymongo/helpers.py使用 a的违规行try-except,但我更喜欢不涉及数据丢失的解决方案。

try:
    result["data"] = bson.decode_all(response[20:], as_class, tz_aware, uuid_subtype)
except:
    result["data"] = []
4

1 回答 1

2

您可以使用 mongo shell 尝试相同的操作吗?我想弄清楚它是特定于 Python 的,还是数据库中的损坏:

$ mongo db-name  
> var collection = db.getCollection('subName.collection-name')  
> collection.find().forEach(function(doc) { printjson(doc); })
于 2012-10-05T21:40:30.640 回答