计算 rethinkDB 中 table 中特定属性值的总和?
calculate sum of values of a specific attribute in a table in rethinkDB?
我的 rethinkdb 数据库中有以下数据集。我想计算一个名为 Min 的属性的总和。我想总结的所有值
分钟。
例如,对于以下数据集,查询应该 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 ,
}
你能帮忙吗?
你不能对数据集使用求和命令吗?大概是这样的-
r.table("tablename").sum("Min").run(conn, callback)
我不熟悉 rethinkDB,但我查看了它的 API,您可以在 sum 命令中找到更多信息 here。
总和:
r.db('dbone').table("usertable").sum("Min")
你遇到错误:
Expected type NUMBER but found OBJECT
with r.db('dbone').table("usertable").filter(r.row("userid").eq("002") ).pluck('Min').sum()
因为在 pluck
之后你得到了对象数组。要在 pluck
的结果上制作 sum
你应该得到这样的字段:
r.db('dbone').table("usertable").filter(r.row("userid").eq("002") ).pluck('Min')("Min").sum()
或将字段名称传递给 sum
:
r.db('dbone').table("usertable").filter(r.row("userid").eq("002") ).pluck('Min').sum("Min")
我的 rethinkdb 数据库中有以下数据集。我想计算一个名为 Min 的属性的总和。我想总结的所有值 分钟。
例如,对于以下数据集,查询应该 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 ,
}
你能帮忙吗?
你不能对数据集使用求和命令吗?大概是这样的-
r.table("tablename").sum("Min").run(conn, callback)
我不熟悉 rethinkDB,但我查看了它的 API,您可以在 sum 命令中找到更多信息 here。
总和:
r.db('dbone').table("usertable").sum("Min")
你遇到错误:
Expected type NUMBER but found OBJECT
with r.db('dbone').table("usertable").filter(r.row("userid").eq("002") ).pluck('Min').sum()
因为在 pluck
之后你得到了对象数组。要在 pluck
的结果上制作 sum
你应该得到这样的字段:
r.db('dbone').table("usertable").filter(r.row("userid").eq("002") ).pluck('Min')("Min").sum()
或将字段名称传递给 sum
:
r.db('dbone').table("usertable").filter(r.row("userid").eq("002") ).pluck('Min').sum("Min")