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

查看:94
本文介绍了使用find()马达[MongoDB + Tornado]时出现BadYieldError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python龙卷风框架的新手.我在MongoDB中收集了一小部分数据.我在python文件中使用了一个简单的get函数.使用db.collection.find()选项时得到BadYieldError.但是db.collection.find_one()可以正常工作,但只显示一条记录.

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))

错误是:

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

tornado.gen.BadYieldError: yielded unknown object MotorCursor()

推荐答案

find返回MotorCursor.产生光标的fetch_next属性以使光标前进,并调用next_object()检索当前文档:

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

请参阅教程部分查询多个文档以获取有关使用Motor的findMotorCursor的说明.

Please refer to the tutorial section Querying for More Than One Document for instructions on using Motor's find and MotorCursor.

这篇关于使用find()马达[MongoDB + Tornado]时出现BadYieldError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆