|
| 1 | +================================= |
| 2 | +MongoDB\\Collection::mapReduce() |
| 3 | +================================= |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Definition |
| 14 | +---------- |
| 15 | + |
| 16 | +.. phpmethod:: MongoDB\\Collection::mapReduce() |
| 17 | + |
| 18 | + The :manual:`mapReduce </reference/command/mapReduce>` command allows you to |
| 19 | + run map-reduce aggregation operations over a collection. |
| 20 | + |
| 21 | + .. code-block:: php |
| 22 | + |
| 23 | + function mapReduce($map, $reduce, $out, array $options = []): MongoDB\MapReduceResult |
| 24 | + |
| 25 | + This method has the following parameters: |
| 26 | + |
| 27 | + .. include:: /includes/apiargs/MongoDBCollection-method-mapReduce-param.rst |
| 28 | + |
| 29 | + The ``$options`` parameter supports the following options: |
| 30 | + |
| 31 | + .. include:: /includes/apiargs/MongoDBCollection-method-mapReduce-option.rst |
| 32 | + |
| 33 | +Return Values |
| 34 | +------------- |
| 35 | + |
| 36 | +A :phpclass:`MongoDB\\MapReduceResult` object, which allows for iteration of |
| 37 | +mapReduce results irrespective of the output method (e.g. inline, collection) |
| 38 | +via the :php:`IteratorAggregate <iteratoraggregate>` interface. It also |
| 39 | +provides access to command statistics. |
| 40 | + |
| 41 | +Errors/Exceptions |
| 42 | +----------------- |
| 43 | + |
| 44 | +.. include:: /includes/extracts/error-unsupportedexception.rst |
| 45 | +.. include:: /includes/extracts/error-invalidargumentexception.rst |
| 46 | +.. include:: /includes/extracts/error-unexpectedvalueexception.rst |
| 47 | +.. include:: /includes/extracts/error-driver-runtimeexception.rst |
| 48 | + |
| 49 | +Behavior |
| 50 | +-------- |
| 51 | + |
| 52 | +In MongoDB, the map-reduce operation can write results to a collection |
| 53 | +or return the results inline. If you write map-reduce output to a |
| 54 | +collection, you can perform subsequent map-reduce operations on the |
| 55 | +same input collection that merge replace, merge, or reduce new results |
| 56 | +with previous results. See :manual:`Map-Reduce </core/map-reduce>` and |
| 57 | +:manual:`Perform Incremental Map-Reduce </tutorial/perform-incremental-map-reduce>` |
| 58 | +for details and examples. |
| 59 | + |
| 60 | +When returning the results of a map-reduce operation *inline*, the |
| 61 | +result documents must be within the :limit:`BSON Document Size` limit, |
| 62 | +which is currently 16 megabytes. |
| 63 | + |
| 64 | +MongoDB supports map-reduce operations on :manual:`sharded collections |
| 65 | +</sharding>`. Map-reduce operations can also output |
| 66 | +the results to a sharded collection. See |
| 67 | +:manual:`Map-Reduce and Sharded Collections </core/map-reduce-sharded-collections>`. |
| 68 | + |
| 69 | +Example |
| 70 | +------- |
| 71 | + |
| 72 | +This example will use city populations to calculate the overall population of |
| 73 | +each state. |
| 74 | + |
| 75 | +.. code-block:: php |
| 76 | + |
| 77 | + <?php |
| 78 | + |
| 79 | + $collection = (new MongoDB\Client)->test->zips; |
| 80 | + |
| 81 | + $map = new MongoDB\BSON\Javascript('function() { emit(this.state, this.pop); }'); |
| 82 | + $reduce = new MongoDB\BSON\Javascript('function(key, values) { return Array.sum(values) }'); |
| 83 | + $out = ['inline' => 1]; |
| 84 | + |
| 85 | + $populations = $collection->mapReduce($map, $reduce, $out); |
| 86 | + |
| 87 | + foreach ($populations as $pop) { |
| 88 | + var_dump($pop); |
| 89 | + }; |
| 90 | + |
| 91 | +The output would then resemble:: |
| 92 | + |
| 93 | + object(stdClass)#2293 (2) { |
| 94 | + ["_id"]=> |
| 95 | + string(2) "AK" |
| 96 | + ["value"]=> |
| 97 | + float(544698) |
| 98 | + } |
| 99 | + object(stdClass)#2300 (2) { |
| 100 | + ["_id"]=> |
| 101 | + string(2) "AL" |
| 102 | + ["value"]=> |
| 103 | + float(4040587) |
| 104 | + } |
| 105 | + object(stdClass)#2293 (2) { |
| 106 | + ["_id"]=> |
| 107 | + string(2) "AR" |
| 108 | + ["value"]=> |
| 109 | + float(2350725) |
| 110 | + } |
| 111 | + object(stdClass)#2300 (2) { |
| 112 | + ["_id"]=> |
| 113 | + string(2) "AZ" |
| 114 | + ["value"]=> |
| 115 | + float(3665228) |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | +See Also |
| 120 | +-------- |
| 121 | + |
| 122 | +- :manual:`mapReduce </reference/command/mapReduce>` command reference in the MongoDB |
| 123 | + manual |
| 124 | +- :manual:`Map-Reduce </core/map-reduce>` documentation in the MongoDB manual |
| 125 | + |
0 commit comments