Skip to content

Commit 6ece2bc

Browse files
committed
Use listCollections for command cursor example, move aggregate to CRUD
1 parent 0a3b827 commit 6ece2bc

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed

docs/classes/collection.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ useCursor (boolean)
131131
### See Also
132132

133133
* [MongoDB Manual: aggregate command](http://docs.mongodb.org/manual/reference/command/aggregate/)
134+
* [MongoDB Manual: Aggregation Pipeline](https://docs.mongodb.org/manual/core/aggregation-pipeline/)
134135

135136
---
136137

docs/tutorial/commands.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,44 +42,42 @@ object(MongoDB\Model\BSONDocument)#2 (1) {
4242

4343
## Iterable Results as a Command Cursor
4444

45-
Some commands, such as [aggregate][aggregate] with the "cursor" option, may
46-
return their results via an iterable command cursor. In this case, the returned
45+
Some commands, such as [listCollections][listcollections], return their results
46+
via an iterable command cursor. In this case, the returned
4747
[MongoDB\Driver\Cursor][cursor] may be iterated in the same manner as one might
4848
do with a [Collection::find()][find] query, like so:
4949

50-
[aggregate]: http://docs.mongodb.org/manual/reference/command/aggregate/
50+
[listcollections]: http://docs.mongodb.org/manual/reference/command/listCollections/
5151
[find]: ../classes/collection.md#find
5252

5353
```
5454
<?php
5555
5656
$database = (new MongoDB\Client)->demo;
5757
58-
$cursor = $database->command([
59-
'aggregate' => 'zips',
60-
'pipeline' => [
61-
['$group' => ['_id' => '$state', 'count' => ['$sum' => 1]]],
62-
['$sort' => ['count' => -1]],
63-
['$limit' => 5],
64-
],
65-
'cursor' => new \stdClass,
66-
]);
58+
$cursor = $database->command(['listCollections' => 1]);
6759
68-
foreach ($cursor as $state) {
69-
printf("%s has %d zip codes\n", $state['_id'], $state['count']);
60+
foreach ($cursor as $collection) {
61+
echo $collection['name'], "\n";
7062
}
7163
```
7264

7365
The above example would output something similar to:
7466

7567
```
76-
TX has 1671 zip codes
77-
NY has 1595 zip codes
78-
CA has 1516 zip codes
79-
PA has 1458 zip codes
80-
IL has 1237 zip codes
68+
persons
69+
posts
70+
zips
8171
```
8272

73+
**Note:** at the protocol level, commands that support a cursor actually return
74+
a single result document with the essential ingredients for constructing the
75+
command cursor (i.e. the cursor's ID, namespace, and first batch of results);
76+
however, the driver's [executeCommand()][executecommand] method already detects
77+
such a result and constructs the iterable command cursor for us.
78+
79+
[executecommand]: http://php.net/manual/en/mongodb-driver-manager.executecommand.php
80+
8381
## Specifying a Read Preference
8482

8583
Some commands, such as [createUser][createUser], can only be executed on a

docs/tutorial/crud.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,48 @@ The above example would output something similar to:
159159
10025: NEW YORK, NY
160160
90201: BELL GARDENS, CA
161161
```
162+
163+
## Aggregation
164+
165+
The [Aggregation Framework][aggregation] may be used to issue complex queries
166+
that filter, transform, and group collection data. The [aggregate()][aggregate]
167+
method returns a [Traversable][traversable] object, which may be iterated
168+
upon to access the results of an aggregation pipeline.
169+
170+
[aggregation]: https://docs.mongodb.org/manual/core/aggregation-pipeline/
171+
[aggregate]: ../classes/collection.md#aggregate
172+
[traversable]: http://php.net/traversable
173+
174+
```
175+
<?php
176+
177+
$collection = (new MongoDB\Client)->demo->zips;
178+
179+
$cursor = $collection->aggregate([
180+
['$group' => ['_id' => '$state', 'count' => ['$sum' => 1]]],
181+
['$sort' => ['count' => -1]],
182+
['$limit' => 5],
183+
]);
184+
185+
foreach ($cursor as $state) {
186+
printf("%s has %d zip codes\n", $state['_id'], $state['count']);
187+
}
188+
```
189+
190+
The above example would output something similar to:
191+
192+
```
193+
TX has 1671 zip codes
194+
NY has 1595 zip codes
195+
CA has 1516 zip codes
196+
PA has 1458 zip codes
197+
IL has 1237 zip codes
198+
```
199+
200+
**Note:** [aggregate()][aggregate] is documented as returning a
201+
[Traversable][traversable] object because the [aggregate][aggregate-cmd] command
202+
may return its results inline (i.e. a single result document's array field,
203+
which the library will package as a PHP iterator) or via a command cursor (i.e.
204+
[MongoDB\Driver\Cursor][cursor]).
205+
206+
[aggregate-cmd]: (http://docs.mongodb.org/manual/reference/command/aggregate/)

0 commit comments

Comments
 (0)