RethinkDB: COUNT Example

In RethinkDB, you can easily count the results, by adding a count predicate:

r.db('test')
 .table('users')
 .count()

If you want to reduce the results first, you can add a filter first:

r.db('test')
 .table('users')
 .filter( (x) => x('last_name').match('test ') )
 .count()

If you want to do a GROUP BY, you can do it with “GROUP”:

r.db('test')
 .table('users')
 .filter( (x) => x('user_name').match('test ') )
 .group('first_name')
 .count()
 .ungroup()
 .map(
   { 
    first_name: r.row('group'),
    val: r.row('reduction') 
  })
 .orderBy('val')

This is an example of what you’ll get:

first_name
val
1
test 10
1
2
test 2
1
test 3
1
4
test 4
3
5
test 5
7
6
test 6
10
7
test 7
15
8
test 8
100
9
test 9
150

Leave a Reply

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