Skip to content

Instantly share code, notes, and snippets.

@coffeemug
Last active March 2, 2022 07:29
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save coffeemug/5894442 to your computer and use it in GitHub Desktop.
Save coffeemug/5894442 to your computer and use it in GitHub Desktop.
Using the `getAll` command to query multiple keys in RethinkDB 1.7.

Querying by multiple keys

The previous releases of RethinkDB allowed querying indexes as follows:

// Three ways to get the user with primary key 5
r.table('users').get(5)
r.table('users').getAll(5)
r.table('users').getAll(5, {index: 'id'})

// Query a secondary index
r.table('users').getAll('Smith', {index: 'last_name'})

However, if you wanted to get all documents from a set of keys, you had to resort to using map:

// Three ways to get the users with primary keys 5 or 6
r.expr([5, 6]).map(r.table('users').get(r.row))
r.expr([5, 6]).map(r.table('users').getAll(r.row))
r.expr([5, 6]).map(r.table('users').getAll(r.row, {index: 'id'}))

// Query a secondary index
r.expr(['Smith', 'Jones']).map(r.table('users').getAll(r.row, {index: 'last_name'}))

As of RethinkDB 1.7, you can now pass multiple arguments to getAll to avoid the cumbersome map syntax above:

// Two ways to get the users with primary keys 5 or 6
r.table('users').getAll(5, 6)
r.table('users').getAll(5, 6, {index: 'id'})

// Query a secondary index
r.table('users').getAll('Smith', 'Jones', {index: 'last_name'})

See more about the 1.7 release in the release announcement.

@verona-rupes
Copy link

Thanks for sharing !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment