5
5
use MongoDB \Collection ;
6
6
use MongoDB \Driver \Command ;
7
7
use MongoDB \Driver \Manager ;
8
+ use MongoDB \Driver \Query ;
8
9
use MongoDB \Driver \ReadPreference ;
9
10
use MongoDB \Driver \Result ;
10
11
use MongoDB \Driver \WriteConcern ;
12
+ use MongoDB \Model \CollectionInfoIterator ;
13
+ use MongoDB \Model \CollectionInfoCommandIterator ;
14
+ use MongoDB \Model \CollectionInfoLegacyIterator ;
15
+ use InvalidArgumentException ;
11
16
12
17
class Database
13
18
{
@@ -84,11 +89,12 @@ public function dropCollection($collectionName)
84
89
*
85
90
* @see http://docs.mongodb.org/manual/reference/command/listCollections/
86
91
* @param array $options
87
- * @return Result
92
+ * @return CollectionInfoIterator
88
93
*/
89
94
public function listCollections (array $ options = array ())
90
95
{
91
- // TODO
96
+ // TODO: Determine if command or legacy method should be used
97
+ return $ this ->listCollectionsCommand ($ options );
92
98
}
93
99
94
100
/**
@@ -110,4 +116,58 @@ public function selectCollection($collectionName, WriteConcern $writeConcern = n
110
116
111
117
return new Collection ($ this ->manager , $ namespace , $ writeConcern , $ readPreference );
112
118
}
119
+
120
+ /**
121
+ * Returns information for all collections in this database using the
122
+ * listCollections command.
123
+ *
124
+ * @param array $options
125
+ * @return CollectionInfoCommandIterator
126
+ */
127
+ private function listCollectionsCommand (array $ options = array ())
128
+ {
129
+ $ command = new Command (array ('listCollections ' => 1 ) + $ options );
130
+ // TODO: Relax RP if connected to a secondary node in standalone mode
131
+ $ readPreference = new ReadPreference (ReadPreference::RP_PRIMARY );
132
+
133
+ $ result = $ this ->manager ->executeCommand ($ this ->databaseName , $ command , $ readPreference );
134
+
135
+ return new CollectionInfoCommandIterator ($ result );
136
+ }
137
+
138
+ /**
139
+ * Returns information for all collections in this database by querying
140
+ * the "system.namespaces" collection (MongoDB <2.8).
141
+ *
142
+ * @param array $options
143
+ * @return CollectionInfoLegacyIterator
144
+ * @throws InvalidArgumentException if the filter option is neither an array
145
+ * nor object, or if filter.name is not a
146
+ * string.
147
+ */
148
+ private function listCollectionsLegacy (array $ options = array ())
149
+ {
150
+ $ filter = array_key_exists ('filter ' , $ options ) ? $ options ['filter ' ] : array ();
151
+
152
+ if ( ! is_array ($ filter ) && ! is_object ($ filter )) {
153
+ throw new InvalidArgumentException (sprintf ('Expected filter to be array or object, %s given ' , gettype ($ filter )));
154
+ }
155
+
156
+ if (array_key_exists ('name ' , $ filter )) {
157
+ if ( ! is_string ($ filter ['name ' ])) {
158
+ throw new InvalidArgumentException (sprintf ('Filter "name" must be a string for MongoDB <2.8, %s given ' , gettype ($ filter ['name ' ])));
159
+ }
160
+
161
+ $ filter ['name ' ] = $ this ->databaseName . '. ' . $ filter ['name ' ];
162
+ }
163
+
164
+ $ namespace = $ this ->databaseName . '.system.namespaces ' ;
165
+ $ query = new Query ($ filter );
166
+ // TODO: Relax RP if connected to a secondary node in standalone mode
167
+ $ readPreference = new ReadPreference (ReadPreference::RP_PRIMARY );
168
+
169
+ $ result = $ this ->manager ->executeQuery ($ namespace , $ query , $ readPreference );
170
+
171
+ return new CollectionInfoLegacyIterator ($ result );
172
+ }
113
173
}
0 commit comments