我是 python tornado 框架的新手。我在 MongoDB 中有一小部分数据。我在我的 python 文件中使用了一个简单的 get 函数。使用 db.collection.find()
选项时出现 BadYieldError
。但是 db.collection.find_one()
工作正常,但它只显示一条记录。
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))
错误是:
tornado.gen.BadYieldError: yielded unknown object MotorCursor()
最佳答案
find
返回一个 MotorCursor
。生成游标的 fetch_next
属性以推进游标并调用 next_object()
以检索当前文档:
@gen.coroutine
def do_find():
cursor = db.test_collection.find({'i': {'$lt': 5}})
while (yield cursor.fetch_next):
document = cursor.next_object()
print document
请引用教程部分Querying for More Than One Document有关使用 Motor 的 find
和 MotorCursor
的说明。
关于python - 使用 find() Motor [MongoDB + Tornado] 时出现 BadYieldError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31421673/