8
8
use MongoDB \Driver \Manager ;
9
9
use MongoDB \Driver \Query ;
10
10
use MongoDB \Driver \ReadPreference ;
11
+ use MongoDB \Driver \Server ;
11
12
use MongoDB \Driver \WriteConcern ;
12
13
use MongoDB \Model \CollectionInfoIterator ;
13
14
use MongoDB \Model \CollectionInfoCommandIterator ;
@@ -97,8 +98,15 @@ public function dropCollection($collectionName)
97
98
*/
98
99
public function listCollections (array $ options = array ())
99
100
{
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 );
102
110
}
103
111
104
112
/**
@@ -125,16 +133,14 @@ public function selectCollection($collectionName, WriteConcern $writeConcern = n
125
133
* Returns information for all collections in this database using the
126
134
* listCollections command.
127
135
*
128
- * @param array $options
136
+ * @param Server $server
137
+ * @param array $options
129
138
* @return CollectionInfoCommandIterator
130
139
*/
131
- private function listCollectionsCommand (array $ options = array ())
140
+ private function listCollectionsCommand (Server $ server , array $ options = array ())
132
141
{
133
142
$ 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 );
138
144
139
145
return new CollectionInfoCommandIterator ($ cursor );
140
146
}
@@ -143,13 +149,14 @@ private function listCollectionsCommand(array $options = array())
143
149
* Returns information for all collections in this database by querying
144
150
* the "system.namespaces" collection (MongoDB <2.8).
145
151
*
146
- * @param array $options
152
+ * @param Server $server
153
+ * @param array $options
147
154
* @return CollectionInfoLegacyIterator
148
155
* @throws InvalidArgumentException if the filter option is neither an array
149
156
* nor object, or if filter.name is not a
150
157
* string.
151
158
*/
152
- private function listCollectionsLegacy (array $ options = array ())
159
+ private function listCollectionsLegacy (Server $ server , array $ options = array ())
153
160
{
154
161
$ filter = array_key_exists ('filter ' , $ options ) ? $ options ['filter ' ] : array ();
155
162
@@ -167,10 +174,7 @@ private function listCollectionsLegacy(array $options = array())
167
174
168
175
$ namespace = $ this ->databaseName . '.system.namespaces ' ;
169
176
$ 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 );
174
178
175
179
return new CollectionInfoLegacyIterator ($ cursor );
176
180
}
0 commit comments