Reputation: 1738
i am trying to use CouchDB as my Database. I have problem when i want to get max value from 50000 records. What the best solution to find max value?
I use this reduce function :
function (key, values, rereduce) {
var largest = Math.max.apply(Math, key);
return largest;
}
And this is my map function :
function(doc) {
if(doc.TYPE=="customer"){
emit(doc._id.substring(0,8), doc._id);
}
}
Thank you
Upvotes: 0
Views: 2589
Reputation: 28429
When writing reduce functions, you should not forget to account for the rereduce
parameter. However, you don't really even need it for 2 reasons:
1) you can just use descending=true
when you query the view and get the largest item. (so you wouldn't need a reduce step at all)
2) you can use the builtin reduce function _stats
and get the max value w/o writing your own reduce function.
The advantage to just using the map function w/o the reduce function is that you can not only get the largest value, but also the document that contains that value.
Upvotes: 1