Skip to content

Instantly share code, notes, and snippets.

@garbados
Created January 14, 2014 21:14
Show Gist options
  • Save garbados/8425852 to your computer and use it in GitHub Desktop.
Save garbados/8425852 to your computer and use it in GitHub Desktop.
Summing values by a date range. For @dj_agiledev :D
{
_id: '_design/queries',
views: {
sum_by_date: {
map: function (doc) {
if (doc.created_at) {
// normalize doc.created_at into an integer timestamp
// in this case, milliseconds since epoch
var date = new Date(doc.created_at).getTime();
emit(doc.created_at, doc.VALUE_YOU_WANT_TO_SUM);
}
},
reduce: '_sum'
}
}
}
GET http://localhost:5984/DATABASE/_design/queries/_view/sum_by_date?startkey=START_TIMESTAMP&endkey=END_TIMESTAMP
@garbados
Copy link
Author

Custom reduce FTW!

@mlmiller
Copy link

I think this is better accomplished by using a built-in reduce and a different key order:

 var date = new Date(doc.date);
 var key = [doc.user_id, date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate()];
 return emit(key, doc.pay);

With a query across dates I get back a result for each user for each day that matches my start and end keys. Is there a way to squash it down even further?

 _view/by_date?group_level=4&startkey=[user_id, 2013,1,14]&endkey=[user_id, 2013,2,15]

Built-in reduces have strong performance gains, are less error prone, and allow you to neat things like:

 emit(key, [doc.pay, doc.hours, ..., <some other numbers>]);

And, then _sum will go and sum each element of the array, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment