MonkeyBonkey
MonkeyBonkey

Reputation: 47881

Does rethinkDB support an in clause?

I couldn't find in the documentation anything about an in clause in rethink.

Does rethink support the equivelent of mongo's $in clause? i.e. row('name').in(['bob', 'tom']) ?

I suppose you can chain a bunch of "or" clauses but that seems clunky.

Upvotes: 1

Views: 170

Answers (1)

kureikain
kureikain

Reputation: 2314

RethinkDB doesn't have in, but it has contains. The order, therefore is in reverse to in. You can write like:

r.expr(['bob', 'tom']).contains(row('name'))

Example,

r.expr([1,2,3,4]).contains(1)
=> true
r.expr([1,2,3,4]).contains(5)
=> false

Another real life example, find an user whose name in an array. Let's create some sample data:

r.tableCreate('test')
r.table('test').insert([{name: 'foo'}, {name: 'bar'}, {name: 'foobar'}])

With above data, I can find all document whose name in an array:

r.table('test').filter(function(doc) {
    return r.expr(['foo', 'bar']).contains(doc('name'))
})

Upvotes: 3

Related Questions