RethinkDB: multiple comparisons filtering

449 Views Asked by At

By the docs, it seems that in order to filter all users that are 30 years old OR 40 years old, I can do this (with python):

r.table("users").filter((r.row["age"].eq(30)) | (r.row["age"].eq(40))).run(conn)

Say I have a list based on input / request: [12, 14, 18, 88, 33 ...], how do I filter all the users that are in the age of one of the elements in the list above by iterating it (and not doing it hard coded)?

1

There are 1 best solutions below

0
On BEST ANSWER

That's one way to do it

valid_ages = [12, 14, 18, 88, 33]

r.table("users").filter(lambda user:
    r.expr(valid_ages).contains(user["age"])
).run(connection)

If you were using an index and get_all, you could do

r.table("users").get_all(*valid_ages, index="age").run(connection)

(You need to create the index age before that)