Sorting keys and mapreduce in couchdb
I am doing a scaled down view for counting user comments for a date range and I am facing a problem not solving the problem. The date range selection is ok - I create an array key [YYYY, MM, DD, user_id].
Sample document:
{
"_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);
}
Decrease function:
_count
So far this is OK - when I want to select all the notes for a 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
Easy to select notes / user / day sums - I change group_level to 4. But what about if I want to count notes for a selected date range with one note sum per user? In this case, I get 28 rows per user, but what about 300 users? It will be 300x28 = 7400 lines (if everyone writes every day :).
The second option replaces the user_id from the last place to the first, but in this case, I need to know the id_user and make a selection for each separately.
Does anyone know how to solve it? :)
a source to share