Gary Sieling

RethinkDB: SELECT DISTINCT example

Using “distinct” in RethinkDB is as simple as adding an additional predicate:

r.db('test')
 .table('salaries')
 .pluck('role')
 .distinct()

3 rows returned in 42ms.

role
1
undefined
2
Designer
3
Manager

You can add as many columns as you need:

r.db('test')
 .table('salaries')
 .pluck('role', 'level')
 .distinct()

7 rows returned in 42ms.

level
role
1
undefined
undefined
2
1
Designer
3
13
Manager
4
2
Designer
5
2
Manager
6
3
Designer
7
3
Manager

Also, unlike some other predicates, this does not transform the data into some weird other data type, so you can operate on it as you would any other:

r.db('test')
 .table('salaries')
 .pluck('role', 'level')
 .distinct()
 .group('role')
 .count()
Exit mobile version