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

Ton*_*czz 1 python json mongodb tornado-motor

我是python龙卷风框架的新手.我在MongoDB中有一小部分数据.我在我的python文件中使用了一个简单的get函数.我BadYieldError在使用该db.collection.find()选项时得到了一个.但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))
Run Code Online (Sandbox Code Playgroud)

错误是:

tornado.gen.BadYieldError:产生未知对象MotorCursor()

A. *_*vis 6

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
Run Code Online (Sandbox Code Playgroud)

有关使用Motor 和的说明,请参阅" 查询多个文档 "教程部分.findMotorCursor