Skip to content

Commit 99fa534

Browse files
committed
PHPLIB-45: List collections according to wire protocol version
1 parent c6b7d7b commit 99fa534

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/Database.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Driver\Manager;
99
use MongoDB\Driver\Query;
1010
use MongoDB\Driver\ReadPreference;
11+
use MongoDB\Driver\Server;
1112
use MongoDB\Driver\WriteConcern;
1213
use MongoDB\Model\CollectionInfoIterator;
1314
use MongoDB\Model\CollectionInfoCommandIterator;
@@ -97,8 +98,15 @@ public function dropCollection($collectionName)
9798
*/
9899
public function listCollections(array $options = array())
99100
{
100-
// TODO: Determine if command or legacy method should be used
101-
return $this->listCollectionsCommand($options);
101+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
102+
$server = $this->manager->selectServer($readPreference);
103+
104+
$serverInfo = $server->getInfo();
105+
$maxWireVersion = isset($serverInfo['maxWireVersion']) ? $serverInfo['maxWireVersion'] : 0;
106+
107+
return ($maxWireVersion >= 3)
108+
? $this->listCollectionsCommand($server, $options)
109+
: $this->listCollectionsLegacy($server, $options);
102110
}
103111

104112
/**
@@ -125,16 +133,14 @@ public function selectCollection($collectionName, WriteConcern $writeConcern = n
125133
* Returns information for all collections in this database using the
126134
* listCollections command.
127135
*
128-
* @param array $options
136+
* @param Server $server
137+
* @param array $options
129138
* @return CollectionInfoCommandIterator
130139
*/
131-
private function listCollectionsCommand(array $options = array())
140+
private function listCollectionsCommand(Server $server, array $options = array())
132141
{
133142
$command = new Command(array('listCollections' => 1) + $options);
134-
// TODO: Relax RP if connected to a secondary node in standalone mode
135-
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
136-
137-
$cursor = $this->manager->executeCommand($this->databaseName, $command, $readPreference);
143+
$cursor = $server->executeCommand($this->databaseName, $command);
138144

139145
return new CollectionInfoCommandIterator($cursor);
140146
}
@@ -143,13 +149,14 @@ private function listCollectionsCommand(array $options = array())
143149
* Returns information for all collections in this database by querying
144150
* the "system.namespaces" collection (MongoDB <2.8).
145151
*
146-
* @param array $options
152+
* @param Server $server
153+
* @param array $options
147154
* @return CollectionInfoLegacyIterator
148155
* @throws InvalidArgumentException if the filter option is neither an array
149156
* nor object, or if filter.name is not a
150157
* string.
151158
*/
152-
private function listCollectionsLegacy(array $options = array())
159+
private function listCollectionsLegacy(Server $server, array $options = array())
153160
{
154161
$filter = array_key_exists('filter', $options) ? $options['filter'] : array();
155162

@@ -167,10 +174,7 @@ private function listCollectionsLegacy(array $options = array())
167174

168175
$namespace = $this->databaseName . '.system.namespaces';
169176
$query = new Query($filter);
170-
// TODO: Relax RP if connected to a secondary node in standalone mode
171-
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
172-
173-
$cursor = $this->manager->executeQuery($namespace, $query, $readPreference);
177+
$cursor = $server->executeQuery($namespace, $query);
174178

175179
return new CollectionInfoLegacyIterator($cursor);
176180
}

0 commit comments

Comments
 (0)