Commit 6ece2bcf authored by Jeremy Mikola's avatar Jeremy Mikola

Use listCollections for command cursor example, move aggregate to CRUD

parent 0a3b8270
...@@ -131,6 +131,7 @@ useCursor (boolean) ...@@ -131,6 +131,7 @@ useCursor (boolean)
### See Also ### See Also
* [MongoDB Manual: aggregate command](http://docs.mongodb.org/manual/reference/command/aggregate/) * [MongoDB Manual: aggregate command](http://docs.mongodb.org/manual/reference/command/aggregate/)
* [MongoDB Manual: Aggregation Pipeline](https://docs.mongodb.org/manual/core/aggregation-pipeline/)
--- ---
......
...@@ -42,12 +42,12 @@ object(MongoDB\Model\BSONDocument)#2 (1) { ...@@ -42,12 +42,12 @@ object(MongoDB\Model\BSONDocument)#2 (1) {
## Iterable Results as a Command Cursor ## Iterable Results as a Command Cursor
Some commands, such as [aggregate][aggregate] with the "cursor" option, may Some commands, such as [listCollections][listcollections], return their results
return their results via an iterable command cursor. In this case, the returned via an iterable command cursor. In this case, the returned
[MongoDB\Driver\Cursor][cursor] may be iterated in the same manner as one might [MongoDB\Driver\Cursor][cursor] may be iterated in the same manner as one might
do with a [Collection::find()][find] query, like so: do with a [Collection::find()][find] query, like so:
[aggregate]: http://docs.mongodb.org/manual/reference/command/aggregate/ [listcollections]: http://docs.mongodb.org/manual/reference/command/listCollections/
[find]: ../classes/collection.md#find [find]: ../classes/collection.md#find
``` ```
...@@ -55,31 +55,29 @@ do with a [Collection::find()][find] query, like so: ...@@ -55,31 +55,29 @@ do with a [Collection::find()][find] query, like so:
$database = (new MongoDB\Client)->demo; $database = (new MongoDB\Client)->demo;
$cursor = $database->command([ $cursor = $database->command(['listCollections' => 1]);
'aggregate' => 'zips',
'pipeline' => [
['$group' => ['_id' => '$state', 'count' => ['$sum' => 1]]],
['$sort' => ['count' => -1]],
['$limit' => 5],
],
'cursor' => new \stdClass,
]);
foreach ($cursor as $state) { foreach ($cursor as $collection) {
printf("%s has %d zip codes\n", $state['_id'], $state['count']); echo $collection['name'], "\n";
} }
``` ```
The above example would output something similar to: The above example would output something similar to:
``` ```
TX has 1671 zip codes persons
NY has 1595 zip codes posts
CA has 1516 zip codes zips
PA has 1458 zip codes
IL has 1237 zip codes
``` ```
**Note:** at the protocol level, commands that support a cursor actually return
a single result document with the essential ingredients for constructing the
command cursor (i.e. the cursor's ID, namespace, and first batch of results);
however, the driver's [executeCommand()][executecommand] method already detects
such a result and constructs the iterable command cursor for us.
[executecommand]: http://php.net/manual/en/mongodb-driver-manager.executecommand.php
## Specifying a Read Preference ## Specifying a Read Preference
Some commands, such as [createUser][createUser], can only be executed on a Some commands, such as [createUser][createUser], can only be executed on a
......
...@@ -159,3 +159,48 @@ The above example would output something similar to: ...@@ -159,3 +159,48 @@ The above example would output something similar to:
10025: NEW YORK, NY 10025: NEW YORK, NY
90201: BELL GARDENS, CA 90201: BELL GARDENS, CA
``` ```
## Aggregation
The [Aggregation Framework][aggregation] may be used to issue complex queries
that filter, transform, and group collection data. The [aggregate()][aggregate]
method returns a [Traversable][traversable] object, which may be iterated
upon to access the results of an aggregation pipeline.
[aggregation]: https://docs.mongodb.org/manual/core/aggregation-pipeline/
[aggregate]: ../classes/collection.md#aggregate
[traversable]: http://php.net/traversable
```
<?php
$collection = (new MongoDB\Client)->demo->zips;
$cursor = $collection->aggregate([
['$group' => ['_id' => '$state', 'count' => ['$sum' => 1]]],
['$sort' => ['count' => -1]],
['$limit' => 5],
]);
foreach ($cursor as $state) {
printf("%s has %d zip codes\n", $state['_id'], $state['count']);
}
```
The above example would output something similar to:
```
TX has 1671 zip codes
NY has 1595 zip codes
CA has 1516 zip codes
PA has 1458 zip codes
IL has 1237 zip codes
```
**Note:** [aggregate()][aggregate] is documented as returning a
[Traversable][traversable] object because the [aggregate][aggregate-cmd] command
may return its results inline (i.e. a single result document's array field,
which the library will package as a PHP iterator) or via a command cursor (i.e.
[MongoDB\Driver\Cursor][cursor]).
[aggregate-cmd]: (http://docs.mongodb.org/manual/reference/command/aggregate/)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment