0

Suppose I have these documents in a Things table:

{
  "name": "Cali",
  "state": "CA"
},
{
  "name": "Vega",
  "state": "NV",
},
{
  "name": "Wash",
  "state": "WA"
}

My UI is a state-picker where the user can select multiple states. I want to display the appropriate results. The SQL equivalent would be:

SELECT * FROM Things WHERE state IN ('CA', 'WA')

I have tried:

r.db('test').table('Things').filter(r.expr(['CA', 'WA']).contains(r('state')))

but that doesn't return anything and I don't understand why that wouldn't have worked.


This works for getting a single state:

r.db('test').table('Things').filter(r.row('state').eq('CA'))
000
  • 26,951
  • 10
  • 71
  • 101

1 Answers1

1
r.db('test').table('Things').filter(r.expr(['CA', 'WA']).contains(r.row('state')))

seems to be working in some versions and returns

[
  {
    "id":  "b20cdcab-35ab-464b-b10b-b2f644df73e6" ,
    "name":  "Cali" ,
    "state":  "CA"
} ,
  {
    "id":  "506a4d1f-3752-409a-8a93-83385eb0a81b" ,
    "name":  "Wash" ,
    "state":  "WA"
  }
]

Anyway, you can use a function instead of r.row:

r.db('test').table('Things').filter(function(row) {
  return r.expr(['CA', 'WA']).contains(row('state'))
})
Kludge
  • 2,653
  • 4
  • 20
  • 42
  • This is what I get when I copy/paste the line in your answer: http://i.imgur.com/DEheCV1.png `e: Cannot use r.row in nested queries. Use functions instead` – 000 Feb 28 '16 at 22:13
  • Edited my answer following your comment :P – Kludge Feb 28 '16 at 22:29
  • Ah, very interesting. The function works. I hoped there would be a more concise approach – 000 Feb 28 '16 at 22:35
  • IIRC it's a matter of version. At some point, r.row usage had been limited – Kludge Feb 28 '16 at 23:06