incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ben Browning <ben...@gmail.com>
Subject Re: Reducing to unique values...
Date Sat, 28 Feb 2009 01:06:19 GMT
Does something as simple as this work?

map:
// This assumes "a" is in a doc field called key and "one" is in a
field called value
function(doc) {
  emit(doc.key, doc.value);
}

reduce:
function(keys, values, rereduce) {
  // rereduce and regular reduce are the
  // same code here since they both just operate
  // on the values array and return an array
  var uniques = [];
  for (var i = 0; i < values.length; i++) {
    if (uniques.indexOf(values[i]) < 0) {
      uniques.push(values[i]);
    }
  }
return uniques;
}

We just construct an array of unique values in the reduce. My simple
testing shows this returns reduce values like

{"rows":[{"key":"a","value":["two","one"]},{"key":"b","value":["three"]}]}

for map values of

{"total_rows":6,"offset":0,"rows":[
{"id":"34ab35d58a1540a24cea02aa6b97d2a6","key":"a","value":"one"},
{"id":"41099d0591233670250ebf146983cade","key":"a","value":"one"},
{"id":"70bd1f511ede201d93655f896ad4c99f","key":"a","value":"one"},
{"id":"c65afdfddfb3ea6a33cfd20e67364d11","key":"a","value":"two"},
{"id":"3b06e1ee7e63cd856aae52513f11bc51","key":"b","value":"three"},
{"id":"611a47c396492eb7b88190e81836427b","key":"b","value":"three"}
]}

Mime
View raw message