Razvan
Razvan

Reputation: 731

Rethinkdb multiple fields with different ordering

Is it possible to do order_by on multiple fields with different ordering?

r.table("users").order_by(r.desc("age") & r.asc("name"))

Upvotes: 0

Views: 297

Answers (1)

Lyubomyr Shaydariv
Lyubomyr Shaydariv

Reputation: 21105

Of course. Just take a look at the RethinkDB orderBy documentation:

table.orderBy([key | function...], {index: index_name}) → table_slice
selection.orderBy(key | function[, ...]) → selection<array>
sequence.orderBy(key | function[, ...]) → array

Note the ellipsis (...) that marks a variadic method allowing arbitrary number of arguments. Therefore your query becomes as following:

r.table("users")
  .order_by(r.desc("age"), r.asc("name"))

Despite the document I've referenced above describes the JavaScript driver implementation, it will go also for other standard drivers (you're probably using Python or Ruby).

Upvotes: 2

Related Questions