How to merge objects attributes from reduce to rereduce function in CouchDB

喜夏-厌秋 提交于 2019-12-04 19:40:53

You're pushing CouchDB pretty close to its limits here — using a reduce function to perform a join and everything.

Your problem comes from the fact that CouchDB may apply zero, one or more rereduce steps, but your code assumes that exactly one rereduce step will be performed. I suspect the null results you get come from the fact that the final rereduce step is applied to some results that come from a reduce step and some results that come from a rereduce step.

Here's a small diagram. M is a map step, R is a reduce step, RR is a rereduce step.

[X] [X] [X] [X] [X] [X] [X] [X] [X] [X] 
 |   |   |   |   |   |   |   |   |   | 
(M) (M) (M) (M) (M) (M) (M) (M) (M) (M)
 |   |   |   |   |   |   |   |   |   | 
(==R==) (==R==) (==R==) (==R==) (==R==)
   |       |       |       |       | 
  (== R R ==)     (== R R ==)      | 
       |               |           | 
      (====== R R ======)          | 
               |                   | 
              (======== R R ========)
                         |
                         v
                        [X]

With CouchDB reduce views, it is essential that the data output by your reduce step has the same format as the data output by your rereduce steps. In particular, this means that instead of storing averages, you need to store (sum,count) pairs.

It would make your life a lot easier if you could put the title and salary in the same employee document together.

{
"name" : "Joe",
"title" : "Plumber",
"salary" : 60000
}

then you could easily emit(doc.title, doc.salary) with the built-in _stats reduce function and get a view of salary stats for each title.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!