You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Carl Bourne <ca...@me.com> on 2012/10/06 20:37:27 UTC

Help with reduce function

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.




Re: Help with reduce function

Posted by Robert Newson <ro...@gmail.com>.
Just in case, it's "startkey" not "startKey", ditto for endkey.

Sent from the ocean floor

On 7 Oct 2012, at 19:42, Carl Bourne <ca...@me.com> wrote:

> Thanks for input Kai,
>
> I gave this a try and it sort of worked. It did summarise all of the countries for me if I didn't pass in the  "startKey", "endKey". However, when I did it did not return any rows.
>
> I've taken a different approach now anyway, I'll do the summary part client side since the document count will be relatively low anyway.
>
> Might also take a look at Riak to see if this offers any additional flexibility in this area!
>
> Thanks for you feedback!
>
>
> On 7 Oct 2012, at 19:09, Kai Griffin <ka...@resourceandrevenue.com> wrote:
>
>> Carl, your problem sounds very familiar to me, but I'm not really sure if it is quite the same as the problem I once had.  I had a fairly complex use-case that involved summarising multiple values from multiple document types, being able to search for these by date (in my case [year, month] or [year, week_nbr]).  So my map/reduce has so much going on, I'm not sure the heart of the issue is really same as yours or not.  I've done my best to distill this down to your use-case... but there's one glaring issue in that my function wants to use the country name as a key, and your country names contain spaces, which won't work as keys.  So, I'm going to assume that they're 2-letter country codes instead.  Also, I'm sure someone might say that I'm abusing the reduce function... there might be a simpler way of doing this with just one value to be summed.
>>
>> map:
>> {
>>   var obj = {};
>>   obj[doc.country] = 1;
>>   emit ([date], obj)
>> }
>>
>>
>> reduce:
>> {
>>   var sums = {};
>>   for (var i in values)
>>      for (var k in values[i])
>>          sums[k] = (sums[k] || 0) + values[i][k];
>>   return sums;
>> }
>>
>> You can query using startkey,endkeys corresponding to the date range, and if you set group_level=0 in your query,  you should end up with something like this:
>>
>> {"key":null, "value":{ UK:3, DE:1, HU:1, FR:2, US:1 }}
>>
>> Which of course isn't quite the structure you might have been hoping for... but it does give the right answer in a single row.
>>
>>
>>
>>
>> On 06/10/2012 23:12, Carl Bourne wrote:
>>> Yes - exactly!
>>>
>>> Which was why I was hoping the reduce function would help. I have managed to do this using some additional middleware (Ruby Sinatra), but that seems to defeat the purpose of using something like Couch in the first place!
>>>
>>> Carl Bourne | Senior Sales Engineer | mobile: +44 (0) 7770 284294 | www.venafi.com
>>>
>>> On 6 Oct 2012, at 21:58, Aurélien Bénel <au...@utt.fr> wrote:
>>>
>>>>> I still not understand why you need to count the docs with the same exact timestamp
>>>> Ah I think I understand now... You want to select by (exact) date but group by countries.
>>>> Hmm, then it's not hierarchical, you have two different dimensions. Then my solution cannot help you. Sorry.
>>>>
>>>>
>>>> Regards,
>>>>
>>>> Aurélien
>

Re: Help with reduce function

Posted by Carl Bourne <ca...@me.com>.
Kia,

Thats interesting! 

My other queries are using dates in the form of startkey="2002-10-02"&endkey="2012-12-02". My dates in the document all look like this "2020-07-28T15:13:00+00:00". 

This seems to work OK. However, does your suggestion regarding Unix dates imply that I doing things incorrectly here too?

Carl



This seems to work OK 

On 8 Oct 2012, at 08:46, Kai Griffin <ka...@resourceandrevenue.com> wrote:

> It have should work equally with the keys... how were you populating the dates in the start & end keys?  (they need to be Unix dates, as in today began at 1344591920).   Admittedly in my case, I split the key up into separate [year, month] so the data could be summarised at that level, which also makes querying on dates a little more straight-forward.
> 
> On 07/10/2012 20:42, Carl Bourne wrote:
>> Thanks for input Kai,
>> 
>> I gave this a try and it sort of worked. It did summarise all of the countries for me if I didn't pass in the  "startKey", "endKey". However, when I did it did not return any rows.
>> 
>> I've taken a different approach now anyway, I'll do the summary part client side since the document count will be relatively low anyway.
>> 
>> Might also take a look at Riak to see if this offers any additional flexibility in this area!
>> 
>> Thanks for you feedback!
>> 
>> 
>> On 7 Oct 2012, at 19:09, Kai Griffin <ka...@resourceandrevenue.com> wrote:
>> 
>>> Carl, your problem sounds very familiar to me, but I'm not really sure if it is quite the same as the problem I once had.  I had a fairly complex use-case that involved summarising multiple values from multiple document types, being able to search for these by date (in my case [year, month] or [year, week_nbr]).  So my map/reduce has so much going on, I'm not sure the heart of the issue is really same as yours or not.  I've done my best to distill this down to your use-case... but there's one glaring issue in that my function wants to use the country name as a key, and your country names contain spaces, which won't work as keys.  So, I'm going to assume that they're 2-letter country codes instead.  Also, I'm sure someone might say that I'm abusing the reduce function... there might be a simpler way of doing this with just one value to be summed.
>>> 
>>> map:
>>> {
>>>    var obj = {};
>>>    obj[doc.country] = 1;
>>>    emit ([date], obj)
>>> }
>>> 
>>> 
>>> reduce:
>>> {
>>>    var sums = {};
>>>    for (var i in values)
>>>       for (var k in values[i])
>>>           sums[k] = (sums[k] || 0) + values[i][k];
>>>    return sums;
>>> }
>>> 
>>> You can query using startkey,endkeys corresponding to the date range, and if you set group_level=0 in your query,  you should end up with something like this:
>>> 
>>> {"key":null, "value":{ UK:3, DE:1, HU:1, FR:2, US:1 }}
>>> 
>>> Which of course isn't quite the structure you might have been hoping for... but it does give the right answer in a single row.
>>> 
>>> 
>>> 
>>> 
>>> On 06/10/2012 23:12, Carl Bourne wrote:
>>>> Yes - exactly!
>>>> 
>>>> Which was why I was hoping the reduce function would help. I have managed to do this using some additional middleware (Ruby Sinatra), but that seems to defeat the purpose of using something like Couch in the first place!
>>>> 
>>>> Carl Bourne | Senior Sales Engineer | mobile: +44 (0) 7770 284294 | www.venafi.com
>>>> 
>>>> On 6 Oct 2012, at 21:58, Aurélien Bénel <au...@utt.fr> wrote:
>>>> 
>>>>>> I still not understand why you need to count the docs with the same exact timestamp
>>>>> Ah I think I understand now... You want to select by (exact) date but group by countries.
>>>>> Hmm, then it's not hierarchical, you have two different dimensions. Then my solution cannot help you. Sorry.
>>>>> 
>>>>> 
>>>>> Regards,
>>>>> 
>>>>> Aurélien
>>>>> 
>>>>> 
>> 
>> 
> 


Re: Help with reduce function

Posted by Kai Griffin <ka...@resourceandrevenue.com>.
It have should work equally with the keys... how were you populating the 
dates in the start & end keys?  (they need to be Unix dates, as in today 
began at 1344591920).   Admittedly in my case, I split the key up into 
separate [year, month] so the data could be summarised at that level, 
which also makes querying on dates a little more straight-forward.

On 07/10/2012 20:42, Carl Bourne wrote:
> Thanks for input Kai,
>
> I gave this a try and it sort of worked. It did summarise all of the countries for me if I didn't pass in the  "startKey", "endKey". However, when I did it did not return any rows.
>
> I've taken a different approach now anyway, I'll do the summary part client side since the document count will be relatively low anyway.
>
> Might also take a look at Riak to see if this offers any additional flexibility in this area!
>
> Thanks for you feedback!
>
>
> On 7 Oct 2012, at 19:09, Kai Griffin <ka...@resourceandrevenue.com> wrote:
>
>> Carl, your problem sounds very familiar to me, but I'm not really sure if it is quite the same as the problem I once had.  I had a fairly complex use-case that involved summarising multiple values from multiple document types, being able to search for these by date (in my case [year, month] or [year, week_nbr]).  So my map/reduce has so much going on, I'm not sure the heart of the issue is really same as yours or not.  I've done my best to distill this down to your use-case... but there's one glaring issue in that my function wants to use the country name as a key, and your country names contain spaces, which won't work as keys.  So, I'm going to assume that they're 2-letter country codes instead.  Also, I'm sure someone might say that I'm abusing the reduce function... there might be a simpler way of doing this with just one value to be summed.
>>
>> map:
>> {
>>     var obj = {};
>>     obj[doc.country] = 1;
>>     emit ([date], obj)
>> }
>>
>>
>> reduce:
>> {
>>     var sums = {};
>>     for (var i in values)
>>        for (var k in values[i])
>>            sums[k] = (sums[k] || 0) + values[i][k];
>>     return sums;
>> }
>>
>> You can query using startkey,endkeys corresponding to the date range, and if you set group_level=0 in your query,  you should end up with something like this:
>>
>> {"key":null, "value":{ UK:3, DE:1, HU:1, FR:2, US:1 }}
>>
>> Which of course isn't quite the structure you might have been hoping for... but it does give the right answer in a single row.
>>
>>
>>
>>
>> On 06/10/2012 23:12, Carl Bourne wrote:
>>> Yes - exactly!
>>>
>>> Which was why I was hoping the reduce function would help. I have managed to do this using some additional middleware (Ruby Sinatra), but that seems to defeat the purpose of using something like Couch in the first place!
>>>
>>> Carl Bourne | Senior Sales Engineer | mobile: +44 (0) 7770 284294 | www.venafi.com
>>>
>>> On 6 Oct 2012, at 21:58, Aurélien Bénel <au...@utt.fr> wrote:
>>>
>>>>> I still not understand why you need to count the docs with the same exact timestamp
>>>> Ah I think I understand now... You want to select by (exact) date but group by countries.
>>>> Hmm, then it's not hierarchical, you have two different dimensions. Then my solution cannot help you. Sorry.
>>>>
>>>>
>>>> Regards,
>>>>
>>>> Aurélien
>>>>
>>>>
>
>


Re: Help with reduce function

Posted by Carl Bourne <ca...@me.com>.
Thanks for input Kai,

I gave this a try and it sort of worked. It did summarise all of the countries for me if I didn't pass in the  "startKey", "endKey". However, when I did it did not return any rows.

I've taken a different approach now anyway, I'll do the summary part client side since the document count will be relatively low anyway.

Might also take a look at Riak to see if this offers any additional flexibility in this area!

Thanks for you feedback!


On 7 Oct 2012, at 19:09, Kai Griffin <ka...@resourceandrevenue.com> wrote:

> Carl, your problem sounds very familiar to me, but I'm not really sure if it is quite the same as the problem I once had.  I had a fairly complex use-case that involved summarising multiple values from multiple document types, being able to search for these by date (in my case [year, month] or [year, week_nbr]).  So my map/reduce has so much going on, I'm not sure the heart of the issue is really same as yours or not.  I've done my best to distill this down to your use-case... but there's one glaring issue in that my function wants to use the country name as a key, and your country names contain spaces, which won't work as keys.  So, I'm going to assume that they're 2-letter country codes instead.  Also, I'm sure someone might say that I'm abusing the reduce function... there might be a simpler way of doing this with just one value to be summed.
> 
> map:
> {
>    var obj = {};
>    obj[doc.country] = 1;
>    emit ([date], obj)
> }
> 
> 
> reduce:
> {
>    var sums = {};
>    for (var i in values)
>       for (var k in values[i])
>           sums[k] = (sums[k] || 0) + values[i][k];
>    return sums;
> }
> 
> You can query using startkey,endkeys corresponding to the date range, and if you set group_level=0 in your query,  you should end up with something like this:
> 
> {"key":null, "value":{ UK:3, DE:1, HU:1, FR:2, US:1 }}
> 
> Which of course isn't quite the structure you might have been hoping for... but it does give the right answer in a single row.
> 
> 
> 
> 
> On 06/10/2012 23:12, Carl Bourne wrote:
>> Yes - exactly!
>> 
>> Which was why I was hoping the reduce function would help. I have managed to do this using some additional middleware (Ruby Sinatra), but that seems to defeat the purpose of using something like Couch in the first place!
>> 
>> Carl Bourne | Senior Sales Engineer | mobile: +44 (0) 7770 284294 | www.venafi.com
>> 
>> On 6 Oct 2012, at 21:58, Aurélien Bénel <au...@utt.fr> wrote:
>> 
>>>> I still not understand why you need to count the docs with the same exact timestamp
>>> Ah I think I understand now... You want to select by (exact) date but group by countries.
>>> Hmm, then it's not hierarchical, you have two different dimensions. Then my solution cannot help you. Sorry.
>>> 
>>> 
>>> Regards,
>>> 
>>> Aurélien
>>> 
>>> 
>> 
> 


Re: Help with reduce function

Posted by Kai Griffin <ka...@resourceandrevenue.com>.
Carl, your problem sounds very familiar to me, but I'm not really sure 
if it is quite the same as the problem I once had.  I had a fairly 
complex use-case that involved summarising multiple values from multiple 
document types, being able to search for these by date (in my case 
[year, month] or [year, week_nbr]).  So my map/reduce has so much going 
on, I'm not sure the heart of the issue is really same as yours or not.  
I've done my best to distill this down to your use-case... but there's 
one glaring issue in that my function wants to use the country name as a 
key, and your country names contain spaces, which won't work as keys.  
So, I'm going to assume that they're 2-letter country codes instead.  
Also, I'm sure someone might say that I'm abusing the reduce function... 
there might be a simpler way of doing this with just one value to be summed.

map:
{
     var obj = {};
     obj[doc.country] = 1;
     emit ([date], obj)
}


reduce:
{
     var sums = {};
     for (var i in values)
        for (var k in values[i])
            sums[k] = (sums[k] || 0) + values[i][k];
     return sums;
}

You can query using startkey,endkeys corresponding to the date range, 
and if you set group_level=0 in your query,  you should end up with 
something like this:

{"key":null, "value":{ UK:3, DE:1, HU:1, FR:2, US:1 }}

Which of course isn't quite the structure you might have been hoping 
for... but it does give the right answer in a single row.




On 06/10/2012 23:12, Carl Bourne wrote:
> Yes - exactly!
>
> Which was why I was hoping the reduce function would help. I have managed to do this using some additional middleware (Ruby Sinatra), but that seems to defeat the purpose of using something like Couch in the first place!
>
> Carl Bourne | Senior Sales Engineer | mobile: +44 (0) 7770 284294 | www.venafi.com
>
> On 6 Oct 2012, at 21:58, Aurélien Bénel <au...@utt.fr> wrote:
>
>>> I still not understand why you need to count the docs with the same exact timestamp
>> Ah I think I understand now... You want to select by (exact) date but group by countries.
>> Hmm, then it's not hierarchical, you have two different dimensions. Then my solution cannot help you. Sorry.
>>
>>
>> Regards,
>>
>> Aurélien
>>
>>
>


Re: Help with reduce function

Posted by Carl Bourne <ca...@me.com>.
Yes - exactly! 

Which was why I was hoping the reduce function would help. I have managed to do this using some additional middleware (Ruby Sinatra), but that seems to defeat the purpose of using something like Couch in the first place!

Carl Bourne | Senior Sales Engineer | mobile: +44 (0) 7770 284294 | www.venafi.com

On 6 Oct 2012, at 21:58, Aurélien Bénel <au...@utt.fr> wrote:

>> I still not understand why you need to count the docs with the same exact timestamp
> 
> Ah I think I understand now... You want to select by (exact) date but group by countries.
> Hmm, then it's not hierarchical, you have two different dimensions. Then my solution cannot help you. Sorry.
> 
> 
> Regards,
> 
> Aurélien
> 
> 

Re: Help with reduce function

Posted by Aurélien Bénel <au...@utt.fr>.
> I still not understand why you need to count the docs with the same exact timestamp

Ah I think I understand now... You want to select by (exact) date but group by countries.
Hmm, then it's not hierarchical, you have two different dimensions. Then my solution cannot help you. Sorry.


Regards,

Aurélien



Re: Help with reduce function

Posted by Aurélien Bénel <au...@utt.fr>.
> Hmm Yes - sort of! Maybe I'm missing something here but the fact that I've created a key based on the date, can you still group/count the occurrences of the country just within the map function? 

Sorry, I still not understand why you need to count the docs with the same exact timestamp, but if you really need this, just get the map and reduce functions I wrote in my first message...


Regards,

Aurélien

Re: Help with reduce function

Posted by Carl Bourne <ca...@me.com>.
Hmm Yes - sort of! Maybe I'm missing something here but the fact that I've created a key based on the date, can you still group/count the occurrences of the country just within the map function? 



Carl Bourne | Senior Sales Engineer | mobile: +44 (0) 7770 284294 | www.venafi.com

On 6 Oct 2012, at 21:31, Aurélien Bénel <au...@utt.fr> wrote:

>> So is it possible to build a reduce function to do this and use the expiry date as the key?
> 
> I don't see why you would need a reduce function for this. 
> Did you try with just a map function? 
> 
> 
> Aurélien

Re: Help with reduce function

Posted by Aurélien Bénel <au...@utt.fr>.
> So is it possible to build a reduce function to do this and use the expiry date as the key? 

I don't see why you would need a reduce function for this. 
Did you try with just a map function? 


Aurélien 

Re: Help with reduce function

Posted by Carl Bourne <ca...@me.com>.
Thanks I thought I was missing something obvious!

So is it possible to build a reduce function to do this and use the expiry date as the key? 

Carl Bourne | Senior Sales Engineer | mobile: +44 (0) 7770 284294 | www.venafi.com

On 6 Oct 2012, at 21:19, Aurélien Bénel <au...@utt.fr> wrote:

>> My documents have expiry dates and I need the ability to build queries that only returns documents that have an expiry date between the startkey and endkey parameters.
> 
> I had no idea of your use case. So no, you don't need any group_level for that. 
> Sorry, I was fooled by your reduce function (I found weird to count docs with the exact same timestamp).
> 
> 
> Regards,
> 
> Aurélien

Re: Help with reduce function

Posted by Aurélien Bénel <au...@utt.fr>.
> My documents have expiry dates and I need the ability to build queries that only returns documents that have an expiry date between the startkey and endkey parameters. 

I had no idea of your use case. So no, you don't need any group_level for that. 
Sorry, I was fooled by your reduce function (I found weird to count docs with the exact same timestamp).


Regards,

Aurélien

Re: Help with reduce function

Posted by Carl Bourne <ca...@me.com>.
Thanks again, but I still not quite sure how this would work for me. 

My documents have expiry dates and I need the ability to build queries that only returns documents that have an expiry date between the startkey and endkey parameters. 

Sorry if I'm being dumb here! 

Carl Bourne | Senior Sales Engineer | mobile: +44 (0) 7770 284294 | www.venafi.com

On 6 Oct 2012, at 20:58, Aurélien Bénel <au...@utt.fr> wrote:

>> I need the ability to query by date. I don't quite understand what you mean by your last comment with regards date. Please could you elaborate?
> 
> If your view is like this:
> ["UK", 2011, 10, 6, 20, 30]        null
> ["UK", 2011, 9, 31, 7, 59]        null
> ["UK", 2012, 10, 6, 21, 31]        null
> ["USA", 2011, 9, 1, 12, 00]        null
> ["USA", 2012, 10, 5, 8, 10]        null
> ["USA", 2012, 10, 6, 20, 03]    null
> ["USA", 2012, 10, 6, 21, 31]    null
> ["USA", 2012, 10, 6, 21, 40]    null
> 
> You can query it with group_level=5 and get:
> ["UK", 2011, 10, 6, 20]        1
> ["UK", 2011, 9, 31, 7]        1
> ["UK", 2012, 10, 6, 21]        1
> ["USA", 2011, 9, 1, 12]        1
> ["USA", 2012, 10, 5, 8]        1
> ["USA", 2012, 10, 6, 20]    1
> ["USA", 2012, 10, 6, 21]    2
> 
> with group_level=4:
> ["UK", 2011, 10, 6]    1
> ["UK", 2011, 9, 31]    1
> ["UK", 2012, 10, 6]    1
> ["USA", 2011, 9, 1]    1
> ["USA", 2012, 10, 5]    1
> ["USA", 2012, 10, 6]    3
> 
> with group_level=3:
> ["UK", 2011, 10]    1
> ["UK", 2011, 9]    1
> ["UK", 2012, 10]    1
> ["USA", 2011, 9]    1
> ["USA", 2012, 10]    4
> 
> with group_level=2:
> ["UK", 2011]        2
> ["UK", 2012]        1
> ["USA", 2011]    1
> ["USA", 2012]    4
> 
> with group_level=1:
> ["UK"]    3
> ["USA"]    5
> 
> So to get all of these stats, you just need to split the date into year, month, hour, minute when you emit the key.
> 
> 
> Regards,
> 
> Aurélien
> 

Re: Help with reduce function

Posted by Aurélien Bénel <au...@utt.fr>.
> I need the ability to query by date. I don't quite understand what you mean by your last comment with regards date. Please could you elaborate?

If your view is like this:
["UK", 2011, 10, 6, 20, 30]		null
["UK", 2011, 9, 31, 7, 59]		null
["UK", 2012, 10, 6, 21, 31]		null
["USA", 2011, 9, 1, 12, 00]		null
["USA", 2012, 10, 5, 8, 10]		null
["USA", 2012, 10, 6, 20, 03]	null
["USA", 2012, 10, 6, 21, 31]	null
["USA", 2012, 10, 6, 21, 40]	null

You can query it with group_level=5 and get:
["UK", 2011, 10, 6, 20]		1
["UK", 2011, 9, 31, 7]		1
["UK", 2012, 10, 6, 21]		1
["USA", 2011, 9, 1, 12]		1
["USA", 2012, 10, 5, 8]		1
["USA", 2012, 10, 6, 20]	1
["USA", 2012, 10, 6, 21]	2

with group_level=4:
["UK", 2011, 10, 6]	1
["UK", 2011, 9, 31]	1
["UK", 2012, 10, 6]	1
["USA", 2011, 9, 1]	1
["USA", 2012, 10, 5]	1
["USA", 2012, 10, 6]	3

with group_level=3:
["UK", 2011, 10]	1
["UK", 2011, 9]	1
["UK", 2012, 10]	1
["USA", 2011, 9]	1
["USA", 2012, 10]	4

with group_level=2:
["UK", 2011]		2
["UK", 2012]		1
["USA", 2011]	1
["USA", 2012]	4

with group_level=1:
["UK"]	3
["USA"]	5

So to get all of these stats, you just need to split the date into year, month, hour, minute when you emit the key.


Regards,

Aurélien


Re: Help with reduce function

Posted by Carl Bourne <ca...@me.com>.
Aurelian,

Thanks for the reply. 

I need the ability to query by date. I don't quite understand what you mean by your last comment with regards date. Please could you elaborate?

Best regards,

Carl

On 6 Oct 2012, at 20:01, Aurélien Bénel <au...@utt.fr> wrote:

> Hi Carl,
> 
> Try always to do more with your index. This is indeed where the power of Map/Reduce lies.
> 
> map:
> function(o) {
>   emit([o.date, o.country], null);
> }
> 
> reduce:
> _count
> 
> Then query it with group=true.
> 
> 
> Regards,
> 
> Aurélien
> 
> P.S. If you did put "date" after "country", and if date were written as an array, you could choose the time slot at query time (year or month, day, hour, etc.) by changing the "group_level".  

Re: Help with reduce function

Posted by Aurélien Bénel <au...@utt.fr>.
Hi Carl,

Try always to do more with your index. This is indeed where the power of Map/Reduce lies.

map:
function(o) {
   emit([o.date, o.country], null);
}

reduce:
_count

Then query it with group=true.


Regards,

Aurélien

P.S. If you did put "date" after "country", and if date were written as an array, you could choose the time slot at query time (year or month, day, hour, etc.) by changing the "group_level".