Also posted here: http://stackoverflow.com/questions/24413833/couchbase-2-2-0-count-of-unique-values
I’m trying to figure out a way to count the number of unique values in a bucket that is not a primary key. Couchbase 2.5 provides a n1ql method to do this. If we use their beer-sample you can issue the following command:
select count(distinct style) from beer-sample
which returns a scalar value of 68.
I’m using couchbase 2.2.0, which technically doesn’t have n1ql. And I want to leverage the map/reduce/rereduce functionality if possible. The reason is that I have 100’s of millions of records and the adhoc query will probably take days to run. Is there such a way to do this?
For the map function I have the following:
function (doc, meta) {
if ( doc.type == “beer”)
emit(doc.style, doc.style);
}
for the reduce I have the following:
function(key, values, rereduce) {
var u = {}, a = [];
var results = {};
if (rereduce) {
for (var i = 0; i < values.length; i ++ ) {
for ( var j = 0; j < values[i].length; j ++ ) {
if (u.hasOwnProperty(values[i][j])) {
continue;
}
a.push(values[i][j]);
u[values[i][j]] = 1;
}
}
return (a);
} else {
for(var i = 0; i < values.length; i++) {
if (u.hasOwnProperty(values[i])) {
continue;
}
a.push(values[i]);
u[values[i]] = 1;
}
return(a);
}
}
This returns an array with unique values but not a scalar count. Any way I can just get the scalar count of unique styles of beers? Thanks.