0

I'd like to do something like this:

r.db('research').table('books').group('year').sum('size_bytes').count().run()

And get a result like this:

{ {"group": 1901, "reduction_size_bytes": 13929238, "reduction_count": 192}, {"group": 1902, "reduction_size_bytes": 21194721, "reduction_count": 223}, ... }

Currently, I only know how to get one "reduction" at a time, e.g. sum of size_bytes:

r.db('research').table('books').group('year').sum('size_bytes').run()

Result:

{ {"group": 1901, "reduction": 13929238}, {"group": 1902, "reduction": 21194721}, ... }

Duane J
  • 1,615
  • 1
  • 15
  • 22

1 Answers1

2

You can run multiple aggregations, but you have to manually do it (you can't use sum for example).

This is what you are looking for:

r.db('research').table('books').group('year').map(function(book) {
  return {
    size_bytes: book("size_bytes"),
    count: 1
  }
}).reduce(function(left, right) {
  return {
    size_bytes: left("size_bytes").add(right("size_bytes")),
    count: left("count").add(right("count"))
  }
}).run()
neumino
  • 4,342
  • 1
  • 18
  • 17
  • Why is it necessary to use "add" instead of "+"? – Duane J Jul 11 '14 at 02:04
  • 1
    The `add` operation is executed on the server, and JavaScript doesn't let you overwrite `+` (basically the driver cannot serialize `+` to the function `add` for the server. – neumino Jul 11 '14 at 02:06