Search code examples
arraysfiltermatchrethinkdbreql

RethinkDB - how to filter records where attribute matches a list / array


RethinkDB

I want to fetch all the records which have a particular field/attribute matching one of 3 different possible values.

In the example below, I want to see all the records who have status equal to 'Ready', 'Active', or 'Something else'

I would expect to be able to do something like this :

r.db('db').table('table').filter(

  function (rec) { 
    return [ 'Ready', 'Active', 'Something else' ].includes( rec('status') );   
  }

);

That doesn't error, nor does it return me all the records that have status equal to 'Ready', 'Active', or 'Something else' (it returns no results).

How do I find all records with a field matching any one of the values in an array?


Solution

  • Another solution to this which is closer to your initial hunch is using .contains(...):

    r.db('db').table('table').filter(
      function (rec) { 
        return r.expr(['Ready', 'Active', 'Something else']).contains( rec('status'));   
      }
    );
    

    This is likely more efficient than using regex.