Skip to content

Commit 2c211e0

Browse files
authored
PHPLIB-577: Add examples for aggregation methods (#797)
* Fix wrong collection name in aggregation example * Add example for MongoDB\Collection::aggregate * Add example for MongoDB\Database::aggregate
1 parent e75156e commit 2c211e0

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

docs/reference/method/MongoDBCollection-aggregate.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,25 @@ MongoDB server version and whether the ``useCursor`` option is specified. If
5858
``result`` array from the command response document. In both cases, the return
5959
value will be :php:`Traversable <traversable>`.
6060

61-
.. todo: add examples
61+
Examples
62+
--------
63+
64+
The following aggregation example uses a collection called ``names`` and groups
65+
the ``first_name`` field together, counts the total number of results in each
66+
group, and sorts the results by name.
67+
68+
.. code-block:: php
69+
70+
<?php
71+
72+
$collection = (new MongoDB\Client)->test->names;
73+
74+
$cursor = $collection->aggregate(
75+
[
76+
['$group' => ['_id' => '$first_name', 'name_count' => ['$sum' => 1]]],
77+
['$sort' => ['_id' => 1]],
78+
]
79+
);
6280

6381
See Also
6482
--------

docs/reference/method/MongoDBDatabase-aggregate.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,25 @@ Errors/Exceptions
5151

5252
.. _php-db-agg-method-behavior:
5353

54-
.. todo: add examples
54+
Examples
55+
--------
56+
57+
The following aggregation example lists all running commands using the
58+
``$currentOp`` aggregation pipeline stage, then filters this list to only show
59+
running command operations.
60+
61+
.. code-block:: php
62+
63+
<?php
64+
65+
$database = (new MongoDB\Client)->admin;
66+
67+
$cursor = $database->aggregate(
68+
[
69+
['$currentOp' => []],
70+
['$match' => ['op' => 'command'],
71+
]
72+
);
5573

5674
See Also
5775
--------

docs/tutorial/collation.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ group, and sorts the results by German phonebook order.
360360

361361
<?php
362362

363-
$collection = (new MongoDB\Client)->test->recipes;
363+
$collection = (new MongoDB\Client)->test->names;
364364

365365
$cursor = $collection->aggregate(
366366
[

0 commit comments

Comments
 (0)