Question

How to get an element of a list at specified index from a rethinkdb document using RQL?

r.db('test').table('user').insert({id: 1, list: ['a','b','c']})
r.db('test').table('user').get(1).do(r.row('list')(0))  // I want to return 'a'

Error:

RqlRuntimeError: Cannot perform get_field on a non-object non-sequence `"a"`. in:
r.db("test").table("user").get(1).do(function(var_18) { return r.row("list")(0); })
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
Was it helpful?

Solution

http://www.rethinkdb.com/api/javascript/#nth

r.db('test').table('user').get(1).do(r.row('list').nth(0)) // returns "a"

OTHER TIPS

A shorter way to do it is with

r.db('test').table('user').get(1)('list').nth(0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top