Reduce function understanding


#1

Maybe you can post the new view code, this will help the users to understand how you solved the issue.
Tug


#2

Sure why not…
My Previous Reduce function

function(key, values,rereduce) {
var r = {};
for(var k in values)
{
r[values[k]] = r.hasOwnProperty(values[k])?r[values[k]]+1:1;
}
return r;
}

My later Reduce function

function(key, values,rereduce) {
var r = {};
for(var k in values)
{
if(rereduce)
{
for(var k1 in values[k])
{
r[k1] = r.hasOwnProperty(k1)?r[k1]+values[k][k1]:values[k][k1];
}
}
else
{
r[values[k]] = r.hasOwnProperty(values[k])?r[values[k]]+1:1;
}
}
return r;
}

Thanks…


#3

Thanks for replying…Solved it…I wasn’t considering rereduce attribute in my reduce function… :frowning: My Mistake…


#4

Hello,
It is not clear to me what you are trying to achieve here.
Could you please give us more information about the dataset you are using and the result you are expecting from you map and reduce functions?
(for example you are not using data coming from your document (doc) or meta data (meta)
Also when working with date you can easily convert your date into an array using the dateToArray built-in function.

emit( dateToArray(doc.yourDate) );

See http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writi
Also most of the time views just need to use the builtins reduce function documented here:
http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writi
Regards
Tug


#5

As per https://groups.google.com/forum/#!topic/couchbase/LQpg3kYvmqc
Hi
I have a map reduce running on set of data where it looks like below.
Map ::
function (doc, meta) {
var a = {“name”:“key_name”,“value”:“value”};
var docDate = new Date();
emit([docDate.getFullYear(),docDate.getMonth(),docDate.getDate()],a);
}
Reduce ::
function(key, values, rereduce) {
return values;
}
Scenario 1 : group = true, level = 3, reduce true
Data set : key:[2012,11,11] ,value: [ { “name”: “key_name”, “value”: “23.0.1271.95”}, { “name”: “key_name”, “value”:“23.0.1271.64”} ]
Scenario 2 : group = true, level = 2, reduce true
Data set : key:[2012,11] ,value: [ [{ “name”: “key_name”, “value”: “23.0.1271.95”}, { “name”: “key_name”, “value”:“23.0.1271.64”}],[{ “name”: “key_name”, “value”: “23.0.1271.95”}, { “name”: “key_name”, “value”:“23.0.1271.64”}, { “name”: “key_name”, “value”:“23.0.1271.64”}, { “name”: “key_name”, “value”:“23.0.1271.64”}] ]
Second scenario has a array inside array when My emitted value is an object. Where am I doing wrong? I am using the latest version of Couchbase.
Thanks…