incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Candler <B.Cand...@pobox.com>
Subject Re: map/reduce function question
Date Tue, 28 Apr 2009 12:18:48 GMT
On Mon, Apr 27, 2009 at 07:32:44PM -0700, Kevin Wang wrote:
> Hi All,
> 
> While I am trying to switch my mind to map/reduce model while accessing
> couchdb data, I found myself struggling.  Here is a simple question that you
> guys might be able to answer:
> 
> If I have the following set of data in a doc:
> 
> {"name": "foo", "prop": "value1"}
> {"name": "bar", "prop": "value2"}
> {"name": "foo", "prop": "value3"}
> {"name": "bob", "prop": "value4"}
> 
> How to write the map/reduce functions so that I can get a view which returns
> the total number of unique name's?  In this case, the result should be 3.

Simple answer:

//MAP
function(doc) {
  if (doc.name) { emit(doc.name, null); }
}

//REDUCE
function(ks, vs, co) {
  if (co) {
    return sum(vs);
  } else {
    return vs.length;
  }
}

Then query the view with group=true and count the number of rows returned.
You also get counts of the objects with each name for free (and you also get
an index by name for free, because you can query the same view with
reduce=false)

Alternatively, you can replace the above reduce function with this one,
which builds a single object that maps names to counts:

//REDUCE
function(ks, vs, co) {
  if (co) {
    var result = vs.shift();
    for (var i in vs) {
      for (var j in vs[i]) {
        result[j] = (result[j] || 0) + vs[i][j];
      }
    }
    return result;
  } else {
    var result = {};
    for (var i in ks) {
      var key = ks[i];
      result[key[0]] = (result[key[0]] || 0) + 1;
    }
    return result;
  }
}

Then the client counts the number of keys in the returned object.

This approach gets the results immediately from the root node without
walking the view index, but becomes inefficient if there are large numbers
of distinct names (say hundreds or thousands) as it builds a very large
reduced object.

HTH,

Brian.

Mime
View raw message