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
Looking for a good programming book? Code Complete influenced me a lot as a programmer trying to write better code.
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 *