javascriptcouchdbfauxton

How to use reduce in Fauxton


I've been following some Couch training, but cannot figure out how to use reduce in Fauxton. Selecting '_count' underneath the map area does nothing by itself. I have tried adding it below the map code, but I guess I need to integrate it somehow. In my example I'm trying to count how many times each tag is used in all documents. This is my view code:

function (doc, meta) {
  if(doc.tags) {
    for(var i in doc.tags) {
      emit(doc.tags[i],1);
    }
  }
}

function (tag, counts) {
  var sum = 0; for ( var i = 0; i < counts.length; i++) { 
    sum += counts[i]; 
  }; 
  return sum; 
}

Solution

  • You put your map function in the map area. Then, you select your reduce function (it can be custom or native reduce functions).

    Then, select your view from the design documents. Click Options and select the Reduce option. Then, run the query and your reduce function should be applied.