问题
I am new to python tornado framework. I have a small collection of data in MongoDB. I am using a simple get function in my python file. I get a BadYieldError when using the db.collection.find() option. But db.collection.find_one() works fine but it display only one record.
import tornado
import bson
from bson import json_util
from bson.json_util import dumps
class TypeList(APIHandler):
@gen.coroutine
def get(self):
doc = yield db.vtype.find()
self.write(json_util.dumps(doc))
The error is:
tornado.gen.BadYieldError: yielded unknown object MotorCursor()
回答1:
find returns a MotorCursor. Yield the cursor's fetch_next property to advance the cursor and call next_object() to retrieve the current document:
@gen.coroutine
def do_find():
cursor = db.test_collection.find({'i': {'$lt': 5}})
while (yield cursor.fetch_next):
document = cursor.next_object()
print document
Please refer to the tutorial section Querying for More Than One Document for instructions on using Motor's find and MotorCursor.
来源:https://stackoverflow.com/questions/31421673/badyielderror-when-using-find-motor-mongodb-tornado