When will rethinkdb return a cursor

守給你的承諾、 提交于 2019-12-04 17:14:36

A driver returns a cursor when the query returns a stream.

Basically when the server produces a stream (a sequence that is lazily computed), the driver will return a cursor. As you fetch rows from the cursor, the server will compute more elements in the sequence.

For example, when you run r.table('xxx'), you will get back a cursor. The server will load the documents from disk as you request them with the driver.

In the JavaScript driver, when a query return an array, the driver will sneak an object that mimics the cursor interface between the arrray itself and Array.prototype. So if query.run(...) returns a sequence, you can just do

query.run(connection).then(function(result) {
    return result.toArray()
}).then(function(result) {
    // do something with result
}).error(function(err) {
    // handle err
})

Basically if you don't want to think if you are getting back a cursor or an array, you can just consider that it's a cursor.

You can read more about stream/cursor here:
http://www.rethinkdb.com/docs/data-types/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!