@@ -4,37 +4,47 @@ Map-Reduce
44
55.. default-domain:: mongodb
66
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.
1211
13- This overview will cover:
12+ .. contents:: This overview will cover:
13+ :backlinks: none
14+ :local:
15+ :depth: 1
1416
15- - :ref:`map-reduce-method`
17+ For many simple aggregation tasks, see the :doc:`aggregation framework
18+ </applications/aggregation>`.
1619
17- - :ref:`map-reduce-examples`
18-
19- - :ref:`map-reduce-incremental`
20-
21- - :ref:`map-reduce-sharded-cluster`
20+ .. _map-reduce-examples:
2221
23- - :ref:`map-reduce-additional-references`
22+ Map-Reduce Examples
23+ -------------------
2424
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:
2627
27- mapReduce()
28- -----------
28+ .. code-block:: javascript
2929
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 .
3347
34- .. _map-reduce-examples:
35-
36- Map-Reduce Examples
37- -------------------
3848.. include:: /includes/examples-map-reduce.rst
3949 :start-after: map-reduce-examples-begin
4050 :end-before: map-reduce-sum-price-wrapper-end
@@ -140,10 +150,10 @@ each day and can be simulated as follows:
140150
141151 var finalizeFunction = function (key, reducedValue) {
142152
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;
145155
146- return reducedValue;
156+ return reducedValue;
147157 };
148158
149159#. Perform map-reduce on the ``session`` collection using the
@@ -154,26 +164,24 @@ each day and can be simulated as follows:
154164
155165 .. code-block:: javascript
156166
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+ )
166174
167175**Subsequent Incremental Map-Reduce**
168176
169177Assume the next day, the ``sessions`` collection grows by the following documents:
170178
171179 .. code-block:: javascript
172180
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 } );
177185
1781865. At the end of the day, perform incremental map-reduce on the
179187 ``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
183191
184192 .. code-block:: javascript
185193
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+ );
195202
196203.. _map-reduce-temporay-collection:
197204
@@ -277,12 +284,9 @@ Additional References
277284
278285.. seealso::
279286
287+ - :doc:`/tutorial/troubleshoot-map-reduce`
288+
280289 - :wiki:`Map-Reduce Concurrency
281290 <How+does+concurrency+work#Howdoesconcurrencywork-MapReduce>`
282291
283292 - `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