如何使用二级索引从游标中只获取一条记录?
r.db('domains').table('info').getAll(domain, {index: 'domain'}).run connection, (err, cursor) ->
throw err if err
cursor.toArray (err, info) ->
throw err if err
callback info
如何只获得一条记录?
可能最简单的方法是像这样修改您的查询:
r.db('domains').table('info').getAll(domain, {index: 'domain'}).limit(1)
这只会给您返回第一个文档。您也只能使用光标中的 1 个文档。但这可能不是那么干净。
有几种方法可以做到这一点。这就是我喜欢做的事情:
R.table('foo') .getAll('bar', {index: 'baz'}) .coerceTo('数组') .run(连接,(错误,结果)=> { 变量结果 如果(错误){ //总是处理错误:) } 结果 = 结果[0] //用你的“结果”做些事情 })