RethinkDB reduce an entire table

If you want to “reduce” an entire table in RethinkDB (combine it all into one record), it does not seem to work directly, like you’d expect.

For instance, this would theoretically count the number of rows:

r.db('Watson')
 .table('description_s_TextGetRankedNamedEntities')
 .reduce(
    function(left, right) {
      return r.add(left, 1)
    }).default(0)

The difficulty is that you’re turning objects into ints, which can’t be done in a single step.

So, in this case you can first turn every row into the number “1”, and then the process works fine:

r.db('Watson')
 .table('description_s_TextGetRankedNamedEntities')
 .map(
   function(row) {
     return 1; 
   }
 )
 .reduce(
    function(left, right) {
      return r.add(left, 1)
    }).default(0)

Similarly, if you want to combine a bunch of arrays into one giant array, you would need to take a similar step (in this case selecting the array out of the table).

r.db('Watson')
 .table('description_s_TextGetRankedNamedEntities')
 .map( 
   function(row) {
     return row('entities');
   }
  ).reduce(
    function(left, right) {
      return r.add(left, right)
    })

Depending on the scenario, you may also find that you want to “group” the rows in advance, and operate on the groups.