calculate sum of values of a specific attribute in a table in rethinkDB?

314 views Asked by At

I have the following dataset in my rethinkdb Database. I want to calculate the sum of one attribute named Min. I want to sum the all values of Min.

For example with the following dataset the query should return 8

{
 "completedAt": Fri Dec 30 2016 10:05:17 GMT+00:00 ,
  "Min": 0 ,
}
{
 "completedAt": Fri Dec 30 2016 10:05:17 GMT+00:00 ,
  "Min": 3 ,
}
{
 "completedAt": Fri Dec 30 2016 10:05:17 GMT+00:00 ,
  "Min": 5 ,
}

Can you help?

2

There are 2 answers

3
Suvitruf - Andrei Apanasik On BEST ANSWER

For sum:

r.db('dbone').table("usertable").sum("Min")

You got error:

Expected type NUMBER but found OBJECT

with r.db('dbone').table("usertable").filter(r.row("userid").eq("‌​002") ).pluck('Min').sum() cause after pluck you get array of objects. To make sum on result of pluck you should get field like this:

r.db('dbone').table("usertable").filter(r.row("userid").eq("‌​002") ).pluck('Min')("Min").sum()

or pass field name to sum:

r.db('dbone').table("usertable").filter(r.row("userid").eq("‌​002") ).pluck('Min').sum("Min")
3
Nikhil Vibhav On

Can you not use a sum command on the dataset? Probably something like this -

r.table("tablename").sum("Min").run(conn, callback)

I'm not familiar with rethinkDB, but I had a look at its APIs and you can find more on the sum command here.