简体   繁体   English

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

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

I am new to python tornado framework. 我是python龙卷风框架的新手。 I have a small collection of data in MongoDB. 我在MongoDB中收集了一小部分数据。 I am using a simple get function in my python file. 我在python文件中使用了一个简单的get函数。 I get a BadYieldError when using the db.collection.find() option. 使用db.collection.find()选项时出现BadYieldError But db.collection.find_one() works fine but it display only one record. 但是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))

The error is: 错误是:

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

find returns a MotorCursor . find返回一个MotorCursor Yield the cursor's fetch_next property to advance the cursor and call next_object() to retrieve the current document: 产生游标的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

Please refer to the tutorial section Querying for More Than One Document for instructions on using Motor's find and MotorCursor . 有关使用Motor's findMotorCursor说明,请参考教程部分“ 查询多个文档 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM