python - 使用 find() Motor [MongoDB + Tornado] 时出现 BadYieldError

标签 python json mongodb tornado-motor

我是 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 的 findMotorCursor 的说明。

关于python - 使用 find() Motor [MongoDB + Tornado] 时出现 BadYieldError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31421673/

相关文章:

python - 如何从项目列表中删除值

javascript - jQuery 按键和值对 JSON 进行计数(这个函数可以工作吗?)

javascript - 如何在Leaflet中引用.json文件?

php - 如何检查一个对象是否为空?

javascript - 从查找或插入获取 ID,MongoDB

python - 在 Python 中,如何重新组织 Excel 中不一致的列?

python - 将多个字典组合成嵌套字典(Python)

python - tensorflow 首次运行问题

java - 如何从文件加载 Java Mongo 驱动程序的 MongoClientOptions?

node.js - 如何在保存后进行 Mongoose 查询?