couchdb erlang reduce-聚合对象 [英] couchdb erlang reduce - aggregate object
问题描述
说我有一个发出以下对象的地图
Say I have a map that emits the following objects
{"basePoints": 2000, "bonusPoints": 1000}
{"basePoints": 1000, "bonusPoints": 50}
{"basePoints": 10000, "bonusPoints": 5000}
我如何在Erlang(不是javascript)中编写一个reduce方法,该方法将返回如下所示的聚合对象:
How could I write a reduce in Erlang (not javascript) that would return an aggregate object like this:
{"basePoints": 13000, "bonusPoints": 6050}
(如果我能帮助的话,我宁愿不必编写2个单独的视图来分别发出每个值)
(I would rather not have to write 2 separate views that emits each value separately if I can help it)
非常感谢!
推荐答案
您实际上不需要特殊的reduce,在这种情况下,您可以使用标准的 _sum
,因为它可以求和不仅是数字,还有数字数组。
You actually do not need special reduce, in this case you can use standard _sum
, since it’s able to sum not only numbers, but also arrays of numbers.
只需发射 [basePointsNum,0]
for basePoints
和 [0,bonusPointsNum]
的 bonusPoints
。或者,如果您在一个文档中同时拥有两个字段,则可能会发出 [basePointsNum,bonusPointsNum]
。
Just emit [basePointsNum, 0]
for basePoints
and [0, bonusPointsNum]
for bonusPoints
. Or if you have both fields in one doc you might emit [basePointsNum, bonusPointsNum]
.
-在 _sum
中,您将收到一个由两个数字组成的数组,每个数字都是相应索引列的总和。该功能似乎尚未公开,但是可同时用于CouchDB和PouchDB,并且运行迅速。
After reducing using built-in _sum
you will receive an array of two numbers, each is a sum of appropriate index column. This feature seems to be undocumented, however works for both CouchDB and PouchDB, and it’s blazing fast.
这篇关于couchdb erlang reduce-聚合对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!