|
| 1 | +********** |
| 2 | +Map-Reduce |
| 3 | +********** |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +:manual:`Map-Reduce </core/map-reduce/>` is a data processing paradigm for |
| 14 | +condensing large volumes of data into aggregated results. |
| 15 | + |
| 16 | +.. note:: |
| 17 | + |
| 18 | + The :ref:`aggregation framework <aggregation>` provides better performance |
| 19 | + and usability than map-reduce operations, and should be preferred for |
| 20 | + new development. |
| 21 | + |
| 22 | +A map-reduce operation is issued on a collection view, as obtained from |
| 23 | +``Collection#find`` method, by calling the ``map_reduce`` method on the |
| 24 | +view. The ``map_reduce`` method takes three arguments: the mapper, the |
| 25 | +reducer and map-reduce options. The mapper and the reducer must be provided |
| 26 | +as strings containing JavaScript functions. |
| 27 | + |
| 28 | +For example, given the following collection with values 1 through 10: |
| 29 | + |
| 30 | +.. code-block:: ruby |
| 31 | + |
| 32 | + coll = client['foo'] |
| 33 | + 10.times do |i| |
| 34 | + coll.insert_one(v: i) |
| 35 | + end |
| 36 | + |
| 37 | +The following invocation will sum up the values less than 6: |
| 38 | + |
| 39 | +.. code-block:: ruby |
| 40 | + |
| 41 | + coll.find(v: {'$lt' => 6}).map_reduce( |
| 42 | + 'function() { emit(null, this.v) }', |
| 43 | + 'function(key, values) { return Array.sum(values) }', |
| 44 | + ).first['value'] |
| 45 | + # => 15.0 |
| 46 | + |
| 47 | +The ``map_reduce`` method returns an instance of |
| 48 | +``Mongo::Collection::View::MapReduce`` - a map-reduce view which holds |
| 49 | +the parameters to be used for the operation. To execute the operation, either |
| 50 | +iterate the results (by using e.g. ``each``, ``first`` or ``to_a`` on the |
| 51 | +view object) or invoke the ``execute`` method. The ``execute`` method issues |
| 52 | +the map-reduce operation but does not return the result set from the server, |
| 53 | +and is primarily useful for when the output of the operation is directed to |
| 54 | +a collection as follows: |
| 55 | + |
| 56 | +.. code-block:: ruby |
| 57 | + |
| 58 | + coll.find(...).map_reduce(...).out('destination_collection').execute |
| 59 | + |
| 60 | +Note that: |
| 61 | + |
| 62 | +- If the results of map-reduce are not directed to a collection, they are |
| 63 | + said to be retrieved inline. In this case the entire result set must fit in |
| 64 | + the 16 MiB BSON document size limit. |
| 65 | +- If the results of map-reduce are directed to a collection, and the |
| 66 | + map-reduce view is iterated, the driver automatically retrieves the |
| 67 | + entire collection and returns its contents as the result set. The |
| 68 | + collection is retrieved without sorting. If map-reduce is performed into |
| 69 | + a collection that is not empty, the driver will return the documents |
| 70 | + as they exist in the collection after the map-reduce operation completes, |
| 71 | + which may include the documents that were in the collection prior to the |
| 72 | + map-reduce operation. |
| 73 | + |
| 74 | +.. code-block:: ruby |
| 75 | + |
| 76 | + coll.find(...).map_reduce(...).out('destination_collection').each do |doc| |
| 77 | + # ... |
| 78 | + end |
| 79 | + |
| 80 | + coll.find(...).map_reduce(...).out(replace: 'destination_collection', db: 'db_name').each do |doc| |
| 81 | + # ... |
| 82 | + end |
| 83 | + |
| 84 | +Given a map-reduce view, it can be configured using the following methods: |
| 85 | + |
| 86 | +.. list-table:: |
| 87 | + :header-rows: 1 |
| 88 | + :widths: 20 80 |
| 89 | + |
| 90 | + * - Method |
| 91 | + - Description |
| 92 | + |
| 93 | + * - ``js_mode`` |
| 94 | + - Sets the ``jsMode`` flag for the operation. |
| 95 | + |
| 96 | + * - ``out`` |
| 97 | + - Directs the output to the specified collection, instead of returning |
| 98 | + the result set. |
| 99 | + |
| 100 | + * - ``scope`` |
| 101 | + - Sets the scope for the operation. |
| 102 | + |
| 103 | + * - ``verbose`` |
| 104 | + - Sets whether to include the timing information in the result. |
| 105 | + |
| 106 | +The following accessor methods are defined on the view object: |
| 107 | + |
| 108 | +.. list-table:: |
| 109 | + :header-rows: 1 |
| 110 | + :widths: 20 80 |
| 111 | + |
| 112 | + * - Method |
| 113 | + - Description |
| 114 | + |
| 115 | + * - ``js_mode`` |
| 116 | + - Returns the current ``jsMode`` flag value. |
| 117 | + |
| 118 | + * - ``map_function`` |
| 119 | + - Returns the map function as a string. |
| 120 | + |
| 121 | + * - ``out`` |
| 122 | + - Returns the current output location for the operation. |
| 123 | + |
| 124 | + * - ``reduce_function`` |
| 125 | + - Returns the reduce function as a string. |
| 126 | + |
| 127 | + * - ``scope`` |
| 128 | + - Returns the current scope for the operation. |
| 129 | + |
| 130 | + * - ``verbose`` |
| 131 | + - Returns whether to include the timing information in the result. |
0 commit comments