0

我有一个数据库。在那我想查看所有已插入的条目。为此,我创建了一个路由“/db”,并在其中添加了以下 RequestHandler。

class dbHandler(RequestHandler):
    @gen.coroutine
    def get(self):
        db = self.settings['db']
        result = yield db.ip.find()
        for res in result:
           self.write(res)

当我打开 /db 路由时,这给了我错误 505。如何获取ip的数据?

4

2 回答 2

1

电机find只返回光标,它不是Future- 它不能被屈服。您可以使用 迭代它fetch_next,或使用它to_list来获取更多数据(或docs中的更多信息)。某种例子

@gen.coroutine
def get(self):
    db = self.settings['db']
    cursor = db.ip.find()
    res = yield cursor.to_list(length=100)
    self.write(res)
于 2015-12-23T21:45:48.127 回答
0

电机是异步的。为了获得类似的数据库操作的结果find,您必须yield使用它返回的 Future 来将 Future 解析为结果:

cursor = db.test_collection.find({'i': {'$lt': 5}}).sort('i')
for document in (yield cursor.to_list(length=100)):
    self.write(str(document))

有关详细信息,请参阅教程:

http://motor.readthedocs.org/en/stable/tutorial-tornado.html#querying-for-more-than-one-document

于 2015-12-23T21:47:21.067 回答