Question

How to get only one record from cursor using secondary indexes?

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

How to get only one record?

Was it helpful?

Solution

Probably the easiest way is to modify your query like so:

r.db('domains').table('info').getAll(domain, {index: 'domain'}).limit(1)

That will give you back only the first document. You could also only consume 1 document from the cursor. But that's probably not quite as clean.

OTHER TIPS

There are a few ways to do this. This is what I like doing:

R.table('foo')
.getAll('bar', {index: 'baz'})
.coerceTo('array')
.run(connection, (err, results) => {

  var result

  if (err) {
    //always handle errors :)
  }

  result = results[0]

  //dosomething with your "result"
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top