Skip to content

Commit 5f5442c

Browse files
committed
PHPLIB-328: Document MapReduceResult class
1 parent 2742baf commit 5f5442c

6 files changed

+313
-2
lines changed

docs/reference/method/MongoDBCollection-mapReduce.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Return Values
3636
-------------
3737

3838
A :phpclass:`MongoDB\\MapReduceResult` object, which allows for iteration of
39-
mapReduce results irrespective of the output method (e.g. inline, collection)
39+
map-reduce results irrespective of the output method (e.g. inline, collection)
4040
via the :php:`IteratorAggregate <iteratoraggregate>` interface. It also
4141
provides access to command statistics.
4242

@@ -117,7 +117,6 @@ The output would then resemble::
117117
float(3665228)
118118
}
119119

120-
121120
See Also
122121
--------
123122

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
=====================================
2+
MongoDB\\MapReduceResult::getCounts()
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\\MapReduceResult::getCounts()
17+
18+
Returns count statistics for the map-reduce operation.
19+
20+
.. code-block:: php
21+
22+
function getCounts(): array
23+
24+
Return Values
25+
-------------
26+
27+
An array of count statistics for the map-reduce operation.
28+
29+
Examples
30+
--------
31+
32+
This example reports the count statistics for a map-reduce operation.
33+
34+
.. code-block:: php
35+
36+
<?php
37+
38+
$collection = (new MongoDB\Client)->test->zips;
39+
40+
$map = new MongoDB\BSON\Javascript('function() { emit(this.state, this.pop); }');
41+
$reduce = new MongoDB\BSON\Javascript('function(key, values) { return Array.sum(values) }');
42+
$out = ['inline' => 1];
43+
44+
$result = $collection->mapReduce($map, $reduce, $out);
45+
46+
var_dump($result->getCounts());
47+
48+
The output would then resemble::
49+
50+
array(4) {
51+
["input"]=>
52+
int(29353)
53+
["emit"]=>
54+
int(29353)
55+
["reduce"]=>
56+
int(180)
57+
["output"]=>
58+
int(51)
59+
}
60+
61+
See Also
62+
--------
63+
64+
- :phpmethod:`MongoDB\\Collection::mapReduce()`
65+
- :manual:`mapReduce </reference/command/mapReduce>` command reference in the
66+
MongoDB manual
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
==============================================
2+
MongoDB\\MapReduceResult::getExecutionTimeMS()
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\\MapReduceResult::getExecutionTimeMS()
17+
18+
Returns the execution time in milliseconds of the map-reduce operation.
19+
20+
.. code-block:: php
21+
22+
function getExecutionTimeMS(): integer
23+
24+
Return Values
25+
-------------
26+
27+
An integer denoting the execution time in milliseconds for the map-reduce
28+
operation.
29+
30+
Examples
31+
--------
32+
33+
This example reports the execution time for a map-reduce operation.
34+
35+
.. code-block:: php
36+
37+
<?php
38+
39+
$collection = (new MongoDB\Client)->test->zips;
40+
41+
$map = new MongoDB\BSON\Javascript('function() { emit(this.state, this.pop); }');
42+
$reduce = new MongoDB\BSON\Javascript('function(key, values) { return Array.sum(values) }');
43+
$out = ['inline' => 1];
44+
45+
$result = $collection->mapReduce($map, $reduce, $out);
46+
47+
var_dump($result->getExecutionTimeMS());
48+
49+
The output would then resemble::
50+
51+
int(244)
52+
53+
See Also
54+
--------
55+
56+
- :phpmethod:`MongoDB\\Collection::mapReduce()`
57+
- :manual:`mapReduce </reference/command/mapReduce>` command reference in the
58+
MongoDB manual
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
=======================================
2+
MongoDB\\MapReduceResult::getIterator()
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\\MapReduceResult::getIterator()
17+
18+
Returns a php:`Traversable <traversable>`, which may be used to iterate
19+
through the results of the map-reduce operation.
20+
21+
.. code-block:: php
22+
23+
function getIterator(): Traversable
24+
25+
Return Values
26+
-------------
27+
28+
A :php:`Traversable <traversable>`, which may be used to iterate through the
29+
results of the map-reduce operation.
30+
31+
Example
32+
-------
33+
34+
This example iterates through the results of a map-reduce operation.
35+
36+
.. code-block:: php
37+
38+
<?php
39+
40+
$collection = (new MongoDB\Client)->test->zips;
41+
42+
$map = new MongoDB\BSON\Javascript('function() { emit(this.state, this.pop); }');
43+
$reduce = new MongoDB\BSON\Javascript('function(key, values) { return Array.sum(values) }');
44+
$out = ['inline' => 1];
45+
46+
$result = $collection->mapReduce($map, $reduce, $out);
47+
48+
foreach ($result as $population) {
49+
var_dump($population);
50+
};
51+
52+
The output would then resemble::
53+
54+
object(stdClass)#2293 (2) {
55+
["_id"]=>
56+
string(2) "AK"
57+
["value"]=>
58+
float(544698)
59+
}
60+
object(stdClass)#2300 (2) {
61+
["_id"]=>
62+
string(2) "AL"
63+
["value"]=>
64+
float(4040587)
65+
}
66+
object(stdClass)#2293 (2) {
67+
["_id"]=>
68+
string(2) "AR"
69+
["value"]=>
70+
float(2350725)
71+
}
72+
object(stdClass)#2300 (2) {
73+
["_id"]=>
74+
string(2) "AZ"
75+
["value"]=>
76+
float(3665228)
77+
}
78+
79+
See Also
80+
--------
81+
82+
- :phpmethod:`MongoDB\\Collection::mapReduce()`
83+
- :php:`IteratorAggregate::getIterator() <manual/en/iteratoraggregate.getiterator.php>`
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
=====================================
2+
MongoDB\\MapReduceResult::getTiming()
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\\MapReduceResult::getTiming()
17+
18+
Returns timing statistics for the map-reduce operation.
19+
20+
.. code-block:: php
21+
22+
function getTiming(): array
23+
24+
Timing statistics will only be available if the ``verbose`` option was
25+
specified for :phpmethod:`MongoDB\\Collection::mapReduce()`.
26+
27+
Return Values
28+
-------------
29+
30+
An array of timing statistics for the map-reduce operation. If no timing
31+
statistics are available, the array will be empty.
32+
33+
Examples
34+
--------
35+
36+
This example specifies the ``verbose`` option for
37+
:phpmethod:`MongoDB\\Collection::mapReduce()` and reports the timing statistics
38+
for a map-reduce operation.
39+
40+
.. code-block:: php
41+
42+
<?php
43+
44+
$collection = (new MongoDB\Client)->test->zips;
45+
46+
$map = new MongoDB\BSON\Javascript('function() { emit(this.state, this.pop); }');
47+
$reduce = new MongoDB\BSON\Javascript('function(key, values) { return Array.sum(values) }');
48+
$out = ['inline' => 1];
49+
50+
$result = $collection->mapReduce($map, $reduce, $out, ['verbose' => true]);
51+
52+
var_dump($result->getTiming());
53+
54+
The output would then resemble::
55+
56+
array(5) {
57+
["mapTime"]=>
58+
int(163)
59+
["emitLoop"]=>
60+
int(233)
61+
["reduceTime"]=>
62+
int(9)
63+
["mode"]=>
64+
string(5) "mixed"
65+
["total"]=>
66+
int(233)
67+
}
68+
69+
See Also
70+
--------
71+
72+
- :phpmethod:`MongoDB\\Collection::mapReduce()`
73+
- :manual:`mapReduce </reference/command/mapReduce>` command reference in the
74+
MongoDB manual

docs/reference/result-classes.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,34 @@ Methods
4040
/reference/method/MongoDBChangeStream-next
4141
/reference/method/MongoDBChangeStream-rewind
4242
/reference/method/MongoDBChangeStream-valid
43+
44+
----
45+
46+
MongoDB\\MapReduceResult
47+
------------------------
48+
49+
.. versionadded:: 1.2
50+
51+
Definition
52+
~~~~~~~~~~
53+
54+
.. phpclass:: MongoDB\\MapReduceResult
55+
56+
This class extends PHP's :php:`IteratorAggregate <iteratoraggregate>`
57+
interface. An instance of this class is returned by
58+
:phpmethod:`MongoDB\\Collection::mapReduce()`.
59+
60+
This class allows for iteration of map-reduce results irrespective of the
61+
output method (e.g. inline, collection). It also provides access to command
62+
statistics.
63+
64+
Methods
65+
~~~~~~~
66+
67+
.. toctree::
68+
:titlesonly:
69+
70+
/reference/method/MongoDBMapReduceResult-getCounts
71+
/reference/method/MongoDBMapReduceResult-getExecutionTimeMS
72+
/reference/method/MongoDBMapReduceResult-getIterator
73+
/reference/method/MongoDBMapReduceResult-getTiming

0 commit comments

Comments
 (0)