UnicodeDecodeError while iterating over MongoDB collection

给你一囗甜甜゛ 提交于 2019-12-11 20:22:23

问题


I am trying to query a MongoDB database using Python 2.7 and pymongo-2.3 using something like this:

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

The iterator fails even though I don't do anything with the entry objects:

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

I'm not the creator of the database I'm trying to query. Does anybody know what I'm doing wrong and how I can fix it? Thanks.


Update: I managed to skip over the offending line from pymongo/helpers.py using a try-except, but I would prefer a solution that does not involve data loss.

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

回答1:


Can you try the same operation using the mongo shell? I want to figure out if it's something Python-specific or if it's corruption in the database:

$ mongo db-name  
> var collection = db.getCollection('subName.collection-name')  
> collection.find().forEach(function(doc) { printjson(doc); })


来源:https://stackoverflow.com/questions/12727692/unicodedecodeerror-while-iterating-over-mongodb-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!