@@ -4,37 +4,47 @@ Map-Reduce
4
4
5
5
.. default-domain:: mongodb
6
6
7
- Map-reduce operations can handle complex aggregation
8
- tasks. [#simple-aggregation-use-framework]_ To perform map-reduce operations,
9
- MongoDB provides the :dbcommand:`mapReduce` command and, in the
10
- :program:`mongo` shell, the wrapper :method:`db.collection.mapReduce()`
11
- method.
7
+ Map-reduce operations can handle complex aggregation tasks. To perform
8
+ map-reduce operations, MongoDB provides the :dbcommand:`mapReduce`
9
+ command and, in the :program:`mongo` shell, the
10
+ :method:`db.collection.mapReduce()` wrapper method.
12
11
13
- This overview will cover:
12
+ .. contents:: This overview will cover:
13
+ :backlinks: none
14
+ :local:
15
+ :depth: 1
14
16
15
- - :ref:`map-reduce-method`
17
+ For many simple aggregation tasks, see the :doc:`aggregation framework
18
+ </applications/aggregation>`.
16
19
17
- - :ref:`map-reduce-examples`
18
-
19
- - :ref:`map-reduce-incremental`
20
-
21
- - :ref:`map-reduce-sharded-cluster`
20
+ .. _map-reduce-examples:
22
21
23
- - :ref:`map-reduce-additional-references`
22
+ Map-Reduce Examples
23
+ -------------------
24
24
25
- .. _map-reduce-method:
25
+ This section provides some map-reduce examples in the :program:`mongo`
26
+ shell using the :method:`db.collection.mapReduce()` method:
26
27
27
- mapReduce()
28
- -----------
28
+ .. code-block:: javascript
29
29
30
- .. include:: /reference/method/db.collection.mapReduce.txt
31
- :start-after: mongodb
32
- :end-before: mapReduce-syntax-end
30
+ db.collection.mapReduce(
31
+ <map>,
32
+ <reduce>,
33
+ {
34
+ <out>,
35
+ <query>,
36
+ <sort>,
37
+ <limit>,
38
+ <finalize>,
39
+ <scope>,
40
+ <jsMode>,
41
+ <verbose>
42
+ }
43
+ )
44
+
45
+ For more information on the parameters, see the
46
+ :method:`db.collection.mapReduce()` reference page .
33
47
34
- .. _map-reduce-examples:
35
-
36
- Map-Reduce Examples
37
- -------------------
38
48
.. include:: /includes/examples-map-reduce.rst
39
49
:start-after: map-reduce-examples-begin
40
50
:end-before: map-reduce-sum-price-wrapper-end
@@ -140,10 +150,10 @@ each day and can be simulated as follows:
140
150
141
151
var finalizeFunction = function (key, reducedValue) {
142
152
143
- if (reducedValue.count > 0)
144
- reducedValue.avg_time = reducedValue.total_time / reducedValue.count;
153
+ if (reducedValue.count > 0)
154
+ reducedValue.avg_time = reducedValue.total_time / reducedValue.count;
145
155
146
- return reducedValue;
156
+ return reducedValue;
147
157
};
148
158
149
159
#. Perform map-reduce on the ``session`` collection using the
@@ -154,26 +164,24 @@ each day and can be simulated as follows:
154
164
155
165
.. code-block:: javascript
156
166
157
- db.runCommand(
158
- {
159
- mapreduce: "sessions",
160
- map: mapFunction,
161
- reduce:reduceFunction,
162
- out: { reduce: "session_stat" },
163
- finalize: finalizeFunction
164
- }
165
- );
167
+ db.sessions.mapReduce( mapFunction,
168
+ reduceFunction,
169
+ {
170
+ out: { reduce: "session_stat" },
171
+ finalize: finalizeFunction
172
+ }
173
+ )
166
174
167
175
**Subsequent Incremental Map-Reduce**
168
176
169
177
Assume the next day, the ``sessions`` collection grows by the following documents:
170
178
171
179
.. code-block:: javascript
172
180
173
- db.session .save( { userid: "a", ts: ISODate('2011-11-05 14:17:00'), length: 100 } );
174
- db.session .save( { userid: "b", ts: ISODate('2011-11-05 14:23:00'), length: 115 } );
175
- db.session .save( { userid: "c", ts: ISODate('2011-11-05 15:02:00'), length: 125 } );
176
- db.session .save( { userid: "d", ts: ISODate('2011-11-05 16:45:00'), length: 55 } );
181
+ db.sessions .save( { userid: "a", ts: ISODate('2011-11-05 14:17:00'), length: 100 } );
182
+ db.sessions .save( { userid: "b", ts: ISODate('2011-11-05 14:23:00'), length: 115 } );
183
+ db.sessions .save( { userid: "c", ts: ISODate('2011-11-05 15:02:00'), length: 125 } );
184
+ db.sessions .save( { userid: "d", ts: ISODate('2011-11-05 16:45:00'), length: 55 } );
177
185
178
186
5. At the end of the day, perform incremental map-reduce on the
179
187
``sessions`` collection but use the ``query`` field to select only the
@@ -183,15 +191,14 @@ Assume the next day, the ``sessions`` collection grows by the following document
183
191
184
192
.. code-block:: javascript
185
193
186
- db.runCommand( {
187
- mapreduce: "sessions",
188
- map: mapFunction,
189
- reduce:reduceFunction,
190
- query: { ts: { $gt: ISODate('2011-11-05 00:00:00') } },
191
- out: { reduce: "session_stat" },
192
- finalize:finalizeFunction
193
- }
194
- );
194
+ db.sessions.mapReduce( mapFunction,
195
+ reduceFunction,
196
+ {
197
+ query: { ts: { $gt: ISODate('2011-11-05 00:00:00') } },
198
+ out: { reduce: "session_stat" },
199
+ finalize: finalizeFunction
200
+ }
201
+ );
195
202
196
203
.. _map-reduce-temporay-collection:
197
204
@@ -277,12 +284,9 @@ Additional References
277
284
278
285
.. seealso::
279
286
287
+ - :doc:`/tutorial/troubleshoot-map-reduce`
288
+
280
289
- :wiki:`Map-Reduce Concurrency
281
290
<How+does+concurrency+work#Howdoesconcurrencywork-MapReduce>`
282
291
283
292
- `MapReduce, Geospatial Indexes, and Other Cool Features <http://www.slideshare.net/mongosf/mapreduce-geospatial-indexing-and-other-cool-features-kristina-chodorow>`_ - Kristina Chodorow at MongoSF (April 2010)
284
-
285
- - :wiki:`Troubleshooting MapReduce`
286
-
287
- .. [#simple-aggregation-use-framework] For many simple aggregation tasks, see the
288
- :doc:`aggregation framework </applications/aggregation>`.
0 commit comments