0

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?

GôTô
  • 7,974
  • 3
  • 32
  • 43
DEO
  • 316
  • 2
  • 4
  • 18

2 Answers2

0

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.

Nikhil Vibhav
  • 120
  • 1
  • 4
  • 11
  • well am getting this for particular UserID , and there are many datasets against eachUserId so when i run the following query **r.db('dbone').table("usertable").filter(r.row("userid").eq("002") ).pluck('Min').sum()** it says Expected type NUMBER but found OBJECT – DEO Jan 05 '17 at 13:24
  • Is `userid` here a string or integer? if its an integer you might have to remove the double quotes there. – Nikhil Vibhav Jan 05 '17 at 13:39
  • well in reql we use double quotes the query runs fine when i dont use sum() – DEO Jan 05 '17 at 13:58
0

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")
  • Thanks it worked like a charm . I didnt thought this way – DEO Jan 09 '17 at 08:00
  • well can you tell me how the sum can be applied if am using a map function like this Bal: r.row('left')('Min').sum('Min') – DEO Jan 12 '17 at 14:09
  • 1
    well i can find the soultion for the summing up the values in a mapping function Bal: r.row('left')('Min').sum('Min') . how it works in a mapping function – DEO Jan 16 '17 at 07:57