linkyndy
linkyndy

Reputation: 17900

Filter by list of ids in RethinkDB

I would like to get all documents in a table with their id matching one in a given list. Something which in SQL would look like: SELECT * FROM table WHERE id IN (1, 2, 3, 4). What's the simplest and most efficient way to do this in RethinkDB?

Upvotes: 2

Views: 1771

Answers (1)

neumino
neumino

Reputation: 4353

If you want to filter by the primary keys

arrayOfIds = [1,2,3,4]
r.table("tableName").getAll(r.args(arrayOfIds)).run(...)

If you want to filter on the field "foo" that is not the primary key

arrayOfIds = [1,2,3,4]
r.table('tableName").indexCreate('foo").run(...)
r.table("tableName").getAll(r.args(arrayOfIds), {index: "foo"}).run(...)

And without an index:

arrayOfIds = [1,2,3,4]
r.table('tableName").filter(function(doc) {
    return r.expr(arrayOfIds).contains(doc("foo"))
}).run(...)

These snippets are written in JavaScript. Use underscore instead of camelCase if you are using Python/Ruby.

Upvotes: 10

Related Questions