RethinkDB: ORDER BY example

You can control the ordering of a query in RethinkDB simple with the orderBy predicate:

r.db('test')
 .table('users')
 .orderBy('last_name')

 

first_name
id
last_name
user_name
1
gary
72b719a2-0a96-4b54-aaba-63b76e5c3c75
sieling
gsieling
2
test
cb93de7e-2d91-48ce-956e-4090694e5cb9
test
test
3
test 1
02b2fa2b-7e50-43cc-a476-a14d3b4324ac
test 1
test
4
test 10
edabe8a9-b77e-4446-97aa-be9b7d542784
test 10
test 10
5
test 2
a86e769d-32fa-4853-9018-f8c360795304
test 2
test 2
6
test 3
bfff0d26-f1f1-4a07-9bb6-e54302c5ee05
test 3
test 3
7
test 4
d6c17523-8857-44c4-a014-12ac58764276
test 4
test 4
8
test 5
3e9345bf-a16b-4bcd-927f-0ff226bf3228
test 5
test 5
9
test 6
a31dd384-cd63-44c8-addd-cb81db15e278
test 6
test 6
10
test 7
d6503bb3-3e85-464e-9019-5beee03d1d9d
test 7
test 7
11
test 8
d83d84fb-88e6-413a-8f13-742f77bed5c5
test 8
test 8
12
test 9
1e1f4b1c-9d72-4473-b997-dfc545bec253
test 9
test 9

You can control the order by using the “desc” function on the global r object:

r.db('test')
 .table('users')
 .orderBy(r.desc('user_name'))

To make case-insensitive searches, you can downcase the result:

r.db('test')
 .table('users')
 .orderBy( x => x('user_name').downcase() )