Database Reference
In-Depth Information
reduce
=
reducef
,
finalize
=
finalizef
,
query
=
query
,
out
=
{
'reduce'
:
'stats.hourly'
})
last_run
=
cutoff
OUTPUT MODES
Here, we're using the
'reduce'
output mode. MongoDB's
mapreduce
provides several of these
modes for different use cases:
replace
In this mode, MongoDB will drop any collection that currently exists with the output name be-
fore writing the
mapreduce
results into it.
merge
In this mode, MongoDB does not drop the output collection first, but will
overwrite
any exist-
ing results with the same key with the results of the
mapreduce
results.
reduce
In this mode, MongoDB treats the output collection as additional input to the reduce phase.
This mode is most useful for incremental aggregation, where we wish to
refine
existing results
based on new data.
inline
In this mode, no output collection is written; the results are returned as the result of the
mapre-
duce
command itself.
The
cutoff
variable allows you to process all events that have occurred since the last run but
before one minute ago. This allows for some delay in logging events. You can safely run this
aggregation as often as you like, provided that you update the
last_run
variable each time.
Since we'll be repeatedly querying the
events
collection by date, it's important to maintain
an index on this property:
>>>
>>>
db
.
events
.
ensure_index
(
'ts'
)