/
map-reduce.txt
81 lines (65 loc) · 3.27 KB
/
map-reduce.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
==========
Map-Reduce
==========
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Map-reduce is a data processing paradigm for condensing large volumes
of data into useful *aggregated* results. For map-reduce operations,
MongoDB provides the :dbcommand:`mapReduce` database command.
Consider the following map-reduce operation:
.. include:: /images/map-reduce.rst
In this map-reduce operation, MongoDB applies the *map* phase to each
input document (i.e. the documents in the collection that match the
query condition). The map function emits key-value pairs. For those
keys that have multiple values, MongoDB applies the *reduce* phase, which
collects and condenses the aggregated data. MongoDB then stores the results
in a collection. Optionally, the output of the reduce function may
pass through a *finalize* function to further condense or process the
results of the aggregation.
All map-reduce functions in MongoDB are JavaScript and run
within the :binary:`~bin.mongod` process. Map-reduce operations take the
documents of a single :term:`collection` as the *input* and can perform
any arbitrary sorting and limiting before beginning the map stage.
:dbcommand:`mapReduce` can return the results of a map-reduce operation
as a document, or may write the results to collections. The input and
the output collections may be sharded.
.. note::
For most aggregation operations, the
:doc:`/core/aggregation-pipeline` provides better performance and
more coherent interface. However, map-reduce operations provide
some flexibility that is not presently available in the aggregation
pipeline.
Map-Reduce JavaScript Functions
-------------------------------
In MongoDB, map-reduce operations use custom JavaScript functions to
*map*, or associate, values to a key. If a key has multiple values
mapped to it, the operation *reduces* the values for the key to a
single object.
The use of custom JavaScript functions provide flexibility to
map-reduce operations. For instance, when processing a document, the
map function can create more than one key and value mapping or no
mapping. Map-reduce operations can also use a custom JavaScript
function to make final modifications to the results at the end of the
map and reduce operation, such as perform additional calculations.
Map-Reduce Behavior
-------------------
In MongoDB, the map-reduce operation can write results to a collection
or return the results inline. If you write map-reduce output to a
collection, you can perform subsequent map-reduce operations on the
same input collection that merge replace, merge, or reduce new results
with previous results. See :dbcommand:`mapReduce` and
:doc:`/tutorial/perform-incremental-map-reduce` for details and
examples.
When returning the results of a map reduce operation *inline*, the
result documents must be within the :limit:`BSON Document Size` limit,
which is currently 16 megabytes. For additional information on limits
and restrictions on map-reduce operations, see the
:doc:`/reference/command/mapReduce` reference page.
MongoDB supports map-reduce operations on :doc:`sharded collections
</core/sharding-introduction>`. Map-reduce operations can also output
the results to a sharded collection. See
:doc:`/core/map-reduce-sharded-collections`.