Duane J
Duane J

Reputation: 1823

Can group have multiple aggregates?

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}, ... }

Upvotes: 0

Views: 531

Answers (1)

neumino
neumino

Reputation: 4353

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

Upvotes: 2

Related Questions