前言:Produce a single value from a sequence through repeated application of a reduction function.
sequence.reduce(function) → value r.reduce(sequence, function) → value
Produce a single value from a sequence through repeated application of a reduction function.
The reduction function can be called on:
two elements of the sequence
one element of the sequence and one result of a previous reduction
two results of previous reductions
The reduction function can be called on the results of two previous reductions because the reducecommand is distributed and parallelized across shards and CPU cores. A common mistaken when using the reducecommand is to suppose that the reduction is executed from left to right. Read the map-reduce in RethinkDBarticle to see an example.
If the sequence is empty, the server will produce a ReqlRuntimeErrorthat can be caught with default.
If the sequence has only one element, the first element will be returned.
Example:Return the numbers of documents in the table posts.
r.table("posts").map{|doc| 1 }
.reduce{ |left, right| left+right }
.default(0).run(conn)
A shorter way to execute this query is to use count.
Example:Suppose that each posthas a field commentsthat is an array of comments.
Return the number of comments for all posts.
r.table("posts").map{|doc|
doc["comments"].count()
}.reduce{ |left, right|
left+right
}.default(0).run(conn)
Example:Suppose that each posthas a field commentsthat is an array of comments.
Return the maximum number comments per post.
r.table("posts").map{|doc|
doc["comments"].count()
}.reduce{ |left, right|
r.branch(
left > right,
left,
right
)
}.default(0).run(conn)
A shorter way to execute this query is to use max.
group
map
concat_map
sum
avg
min
max
Couldn't find what you were looking for?
Ask a questionon Stack Overflow
Chat with usand our community on Slack
Talk to the team on IRCon #rethinkdb@freenode.net— via Webchat
Ping@rethinkdbon Twitter
Post an issueon the documentation issue trackeron GitHub
文章出自:http://qh.itpxw.cn/qgtk/201947282.html
文章标题:reduce是什么
免责声明:本站文章均由入驻起航学习网的会员所发或者网络转载,所述观点仅代表作者本人,不代表起航学习网立场。如有侵权或者其他问题,请联系举报,必删。侵权投诉
IT培训网 访问该机构站点 报名留言 加为好友 用户等级:注册会员
用户级别:10
机构名称:IT培训网
联 系 人:罗老师
联系电话:13783581536
联系手机:13783581536
在线客服:
在 线 QQ:
电子邮件:
网站域名:http://www.itpxw.cn
注册时间:2016-07-18 11:07
最后登录:2024-02-20 13:02
胡贝儿国际形体礼仪怎么样 胡贝儿国际形体礼仪女子学堂怎么...
单位职务怎么填写才是正确的 很多小伙伴在填资料表的时候都...
大夫和医生两个称谓有什么区别 很多小伙伴在医院看病的时候...
高铁一般都在什么招收乘务员 坐飞机和高铁离不开的就是乘务...