couchdbcouchdb-futondatabasenosql

Counting couchdb rows unique only


I have a db in couch with 55,000,000 docs. Many of the docs have duplicate values for certain properties and I would like to get a count of only unique values for a property.

I'm new to couchdb and saw list function but this is far too slow for iterating over 55 million rows and times out.

If I do:

"map": "function(doc) { if (doc.property) { emit(doc.property, 1); } }" "reduce": "_count"

and then group, I get the total count of property including duplicates. How can I get this reduced to uniques only?

Thanks.


Solution

  • Your map function is OK - you can't do better here. Let's focus on reduce.

    function(keys, values) {
      var result = {};
      var counter = 0;
      keys.forEach(function(key) { 
        if (!result[key]) {
          result[key] = true; // or whatever
          counter++;
        }
      });
    
      return counter;
    }