CouchDB通过一组值减少函数 [英] CouchDB reduce function with an array of values
问题描述
我有一个map函数,该函数以数组形式返回值:
I have a map function that returns a value as an array:
emit(doc.created_date, { calories : doc.calories, miles : doc.miles, minutes : doc.minutes, reps : doc.reps, steps : doc.steps, water : doc.water })
我想对所有返回值的卡路里,英里,分钟和步长求和.喜欢
I want to run a sum on the calories, miles, minutes and steps for all the returned values. Like
return {"calories":t_cal,"miles":t_mil, "minutes":t_min,"steps":t_step};
我已经尝试了沙发Wiki和其他站点的几个示例,但是我不知道如何访问值数组.
I have tried several of the examples the couch wiki and other sites, but i cannot figure out how to access the value array.
当我尝试对值进行求和或在运行for循环时为null时,得到reduce_overflow_error
:
I get reduce_overflow_error
when i try just summing the values or a null when running a for loop:
for(var i in values) { t_mil = t_mil + values[i].miles }
推荐答案
我正在做很多事情,所以有可能.
I do what you are doing a lot, so it is possible.
您的对象只是超出CouchDB限制的一点,或者代码中存在错误.
Either your object is a little bit over CouchDB's limit, or there is a bug in your code.
您可以设置CouchDB配置query_server_config/reduce_limit = "false"
并查看其外观.
You can set the CouchDB config, query_server_config/reduce_limit = "false"
and see how it looks.
但是,如果您仅累积四个项目,那么我认为这不是减少限制的问题.我总是会遇到JavaScript问题.例如,在字符串上添加数字会产生一个(更长)的字符串.添加更多数字会使字符串越来越长.
However, if you are only accumulating four items, I do not think it is a reduce limit issue. What always happens to me is JavaScript problems. For example, adding a number to a string produces a (longer) string. Adding more numbers makes the string longer and longer.
var old_value = "3" // This is a bad value, perhaps from a map function bug
var new_value = 5
new_value = new_value + old_value // I wanted 8, but I got "53"
new_value = new_value + 2012 // I wanted 2020 but I got "532012"
数组和其他类型也发生类似的问题.
Similar problems occur for arrays and other types.
您可以从map
函数开始,该函数发出与reduce
返回的相同的东西(一个对象).也许您可以发布一些您正在使用的代码.我通常会这样:
You have a good start with the map
function emitting the same thing that reduce
returns (an object). Perhaps you could post some code that you are using. I usually do something like this:
function(keys, vals, rereduce) {
// reduce function
var result = {'calories':0, 'miles':0, 'minutes':0, 'steps':0}
for(var i = 0; i < vals.length; i++) {
result.calories += vals[i].calories || 0
result.miles += vals[i].miles || 0
result.minutes += vals[i].minutes || 0
result.steps += vals[i].steps || 0
}
return result
}
请注意reduce的输出与map的输出完全相同,因此此代码可用于reduce和re-reducing.
Notice that the reduce output is exactly the same as the map output, so this code works for reducing and re-reducing.
这篇关于CouchDB通过一组值减少函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!