Skip to content

PHPLIB-577: Add examples for aggregation methods #797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion docs/reference/method/MongoDBCollection-aggregate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,25 @@ MongoDB server version and whether the ``useCursor`` option is specified. If
``result`` array from the command response document. In both cases, the return
value will be :php:`Traversable <traversable>`.

.. todo: add examples
Examples
--------

The following aggregation example uses a collection called ``names`` and groups
the ``first_name`` field together, counts the total number of results in each
group, and sorts the results by name.

.. code-block:: php

<?php

$collection = (new MongoDB\Client)->test->names;

$cursor = $collection->aggregate(
[
['$group' => ['_id' => '$first_name', 'name_count' => ['$sum' => 1]]],
['$sort' => ['_id' => 1]],
]
);

See Also
--------
Expand Down
20 changes: 19 additions & 1 deletion docs/reference/method/MongoDBDatabase-aggregate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,25 @@ Errors/Exceptions

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

.. todo: add examples
Examples
--------

The following aggregation example lists all running commands using the
``$currentOp`` aggregation pipeline stage, then filters this list to only show
running command operations.

.. code-block:: php

<?php

$database = (new MongoDB\Client)->admin;

$cursor = $database->aggregate(
[
['$currentOp' => []],
['$match' => ['op' => 'command'],
]
);

See Also
--------
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/collation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ group, and sorts the results by German phonebook order.

<?php

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

$cursor = $collection->aggregate(
[
Expand Down