@@ -42,44 +42,42 @@ object(MongoDB\Model\BSONDocument)#2 (1) {
42
42
43
43
## Iterable Results as a Command Cursor
44
44
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
47
47
[ MongoDB\Driver\Cursor] [ cursor ] may be iterated in the same manner as one might
48
48
do with a [ Collection::find()] [ find ] query, like so:
49
49
50
- [ aggregate ] : http://docs.mongodb.org/manual/reference/command/aggregate /
50
+ [ listcollections ] : http://docs.mongodb.org/manual/reference/command/listCollections /
51
51
[ find ] : ../classes/collection.md#find
52
52
53
53
```
54
54
<?php
55
55
56
56
$database = (new MongoDB\Client)->demo;
57
57
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]);
67
59
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" ;
70
62
}
71
63
```
72
64
73
65
The above example would output something similar to:
74
66
75
67
```
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
81
71
```
82
72
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
+
83
81
## Specifying a Read Preference
84
82
85
83
Some commands, such as [ createUser] [ createUser ] , can only be executed on a
0 commit comments