Filter where attribute is in supplied array

104 views Asked by At

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'))
1

There are 1 answers

4
Kludge On BEST ANSWER
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'))
})