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()

Interested in JavaScript? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *