couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Carl Bourne <carl.bou...@me.com>
Subject Help with reduce function
Date Sat, 06 Oct 2012 18:37:27 GMT
Hi,

I have the following map function:

function(doc) {
 {
     emit(doc.date, {'country':doc.country, 'number':1});
 }


which emits data something like this:

{"total_rows":55717,"offset":0,"rows":[
{"id":"338b79f07dfe8b3877b3aa41a5bb8a58","key":"2000-06-23T23:59:00+00:00","value":{"country":"United
Kingdom", "number":1}},
{"id":"338b79f07dfe8b3877b3aa41a5bb983e","key":"2000-06-23T23:59:00+00:00","value":{"country":"United
States", "number":1}},
{"id":"338b79f07dfe8b3877b3aa41a5ddfefe","key":"2000-06-23T23:59:00+00:00","value":{"country":"Hungary",
"number":1}},
{"id":"338b79f07dfe8b3877b3aa41a5fe29d7","key":"2000-06-23T23:59:00+00:00","value":{"country":"Germany",
"number":1}},
{"id":"b6ed02fb38d6506d7371c419751e8a14","key":"2000-06-23T23:59:00+00:00","value":{"country":"France",
"number":1}},
{"id":"b6ed02fb38d6506d7371c419753e20b6","key":"2000-06-23T23:59:00+00:00","value":{"country":"United
Kingdom", "number":1}},
{"id":"b6ed02fb38d6506d7371c419755f34ad","key":"2000-06-23T23:59:00+00:00","value":{"country":"United
States", "number":1}},
{"id":"b6ed02fb38d6506d7371c419755f3e17","key":"2000-06-23T23:59:00+00:00","value":{"country":"United
KingdomA", "number":1}},
{"id":"338b79f07dfe8b3877b3aa41a506082f","key":"2003-01-08T19:34:00+00:00","value":{"country":"France",
"number":1}},
{"id":"9366afb036bf8b63c9f45379bbe29509","key":"2003-01-08T19:34:00+00:00","value":{"country":"Germany",
"number":1}}
]
}
I'm trying to build a simple reduce function to reduce the data down to this:

{"Germany"=>"2",
 "United Kingdom"=>"3",
 "Hungary"=>"1",
 "United States"=>"2",
 "France"=>"1"
}
So basically count the number of countries but I need the ability to use the "StartKey" and
"EndKey" parameters in couch to define the date ranges the query should apply to.


I've built this reduce function and tried several variations of it but can't get it to work.
Am I missing something?

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

If I change my map function to look like this:

function(doc) {
 {
     emit('country':doc.country, 1);
 }

And use this reduce function:

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

It works as expected.

This is driving me nuts so any help would be much appreciated.




Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message