views:

161

answers:

1

Hi all, I am making reduce view for counting users notes for date range and I am in front of quite not-solving problem. Date range select is ok - I create array key [YYYY,MM,DD,user_id].

Document example:

{
   "_id": "1",
   "_rev": "1-84d3fa5f259c7b30c74028ca60a45f91",
   "body": "note text",
   "user_id": 666,
   "created_at": "Thu Apr 26 13:50:20 +0200 2010",
}

Map function:

function(doc){
   var created = new Date(doc.created_at);
   emit([created.getFullYear(),created.getMonth()+1,created.getDate(),doc.user_id],doc);
}

Reduce function:

_count

Until this OK - when I want to select all notes for selected date range, for example from 2010-02-02 to 2010-03-03:

reduce_view_name?startkey=[2010,02,01]&endkey=[2010,02,28,{}]&group_level=0

It is easy select sums notes/user/day - I change group_level to 4. But what about I want count notes for selected date range with one notes sum per user? In this case I get 28 rows per user, but what about if you have 300 users? It will be 300x28=7400 rows (if everybody will be write everyday:).

2nd option is replace user_id from last place to the 1st, but in this case I need know id_user and make select for everyone separately.

Does anybody idea how to solve it?:)

A: 

Yes: add another view that has the key (from the map view) inverted, so that the username comes first and the date comes after that. I know it's a little redundant, but it works like crazy. ;)

djc
Hi, I did it 2 weeks ago as I wrote at the end of my ask:2nd option is replace user_id from last place to the 1st, but in this case I need know id_user and make select for everyone separately.It wasn't another reason I think:-/
podolinek