[英]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
find
和MotorCursor
说明,请参考教程部分“ 查询多个文档 ”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.