Database Reference
In-Depth Information
1, 0, 0, 0, 0) };
emit(
key,
{
total: this.value.total,
count: this.value.count,
mean: 0,
ts: null });
}''' )
These map functions are identical to each other except for the date calculation.
To make our aggregation at these levels efficient, we need to create indexes on the value.ts
field in each collection that serves as input to an aggregation:
>>>
>>> db . stats . daily . ensure_index ( 'value.ts' )
>>>
>>> db . stats . monthly . ensure_index ( 'value.ts' )
Refactor map functions
Using Python's string interpolation, we can refactor the map function definitions as follows:
mapf_hierarchical = '''function() {
var key = {
u: this._id.u,
d: %s };
emit(
key,
{
total: this.value.total,
count: this.value.count,
mean: 0,
ts: null });
}'''
mapf_day = bson . Code (
mapf_hierarchical % '''new Date(
this._id.d.getFullYear(),
this._id.d.getMonth(),
this._id.d.getDate(),
0, 0, 0, 0)''' )
mapf_week = bson . Code (
Search WWH ::




Custom Search