Database Reference
In-Depth Information
out = { 'reduce' : 'stats.daily' })
last_run = cutoff
Thereareacoupleofthingstonotehere.Firstofall,thequeryisnoton ts now,but value.ts ,
the timestamp written during the finalization of the hourly aggregates. Also note that we are,
in fact, aggregating from the stats.hourly collection into the stats.daily collection.
Because we'll be running this query on a regular basis, and the query depends on the
value.ts field, we'll want to create an index on value.ts :
>>>
>>> db . stats . hourly . ensure_index ( 'value.ts' )
Weekly and monthly aggregation
We can use the aggregated day-level data to generate weekly and monthly statistics. A map
function for generating weekly data follows:
mapf_week = bson . Code ( '''function() {
var key = {
u: this._id.u,
d: new Date(
this._id.d.valueOf()
- dt.getDay()*24*60*60*1000) };
emit(
key,
{
total: this.value.total,
count: this.value.count,
mean: 0,
ts: null });
}''' )
Here, to get the group key, the function takes the current day and subtracts days until you get
the beginning of the week. In the monthly map function, we'll use the first day of the month
as the group key, as follows:
mapf_month = bson . Code ( '''function() {
d: new Date(
this._id.d.getFullYear(),
this._id.d.getMonth(),
Search WWH ::




Custom Search