I’ve got a view on my bucket with the built-in _count reduce function. If I set group_level to 1 I get all of the unique key, together with a count of docs with that key, which is great. However, there is no metadata with a total_rows count. This would be really useful to find out how many unique keys there are without having to get all of the keys.
If I remove group_level and set “reduce” to false, the query does return total_rows (I can even set limit to 0 so that I don’t actually get any of the values, which returns a very quick result). However, this is the total_rows of all docs output from the map function, not just the unique keys.
I can see two possible solutions to this:
- Read all of the unique keys (group_level 1) and call rows.length. As there could be several hundred thousand unique keys I really don’t want to do this just to find out how many unique keys there are, mostly because it’s slow with a lot of keys.
- Use a custom reduce function, similar to the _count function, but just returning 1 for the value on the first pass, then count the results on the rereduce pass (I don’t care about the count of each unique values, I just want to know how many there are).
function(key, values, rereduce) {
var retval;
if (!rereduce) {
retval = 1;
} else {
retval = 0;
for (i = 0; i < values.length; i++) {
retval += values[i];
}
}
return retval;
}
However, this does not work! In my current data there are 32,628 unique keys. However, the reduce from this function (group_level = 0) returns 2620.
What is wrong with this reduce function? Or is there another solution to this problem? It doesn’t seem to me like an unusual use case…?
Thanks,
Giles