BadYieldError when using find() Motor [MongoDB + Tornado]

可紊 提交于 2019-12-02 12:48:25

问题


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

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