Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RethinkDB filter and retrieve value from nested array

Tags:

rethinkdb

Using the following query:

r.db('somedb').table('sometable')('users')

I get the following data from the result:

[
   [
      {
         "fn": "dpw",
         "u": "usertwo"
      },
      {
         "fn": "dwd",
         "u": "userone"
      }
   ]
]

I would like to take the field "u" and specify lets say "usertwo" and get the value of "fn" for that "u". I want to have the result filtered using ReQL so that I am not just parsing the json result in nodejs as the result will be enormous eventually. What would be the best and most efficient approach. I am new to RethinkDB and would appreciate if you could explain the answer as best you can.

like image 291
Default Avatar asked Aug 02 '14 01:08

Default


1 Answers

I'm not sure of what you exactly want, but from my understanding, this is what you are looking for:

r.db('somedb').table('sometable')('users').filter(function(user) {
    return user("u").eq("usertwo")
})("fn")

You seem to have an array of array of users. if that was not a typo, the query should probably be

r.db('somedb').table('sometable')('users').nth(0).filter(function(user) {
    return user("u").eq("usertwo")
})("fn")
like image 52
neumino Avatar answered Sep 21 '22 00:09

neumino