|
16 | 16 | use MongoDB\Model\IndexInfoIteratorIterator;
|
17 | 17 | use MongoDB\Model\IndexInput;
|
18 | 18 | use MongoDB\Operation\Aggregate;
|
| 19 | +use MongoDB\Operation\CreateIndexes; |
19 | 20 | use MongoDB\Operation\Distinct;
|
20 | 21 | use Traversable;
|
21 | 22 |
|
@@ -262,8 +263,6 @@ public function count(array $filter = array(), array $options = array())
|
262 | 263 | /**
|
263 | 264 | * Create a single index for the collection.
|
264 | 265 | *
|
265 |
| - * @see http://docs.mongodb.org/manual/reference/command/createIndexes/ |
266 |
| - * @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/ |
267 | 266 | * @see Collection::createIndexes()
|
268 | 267 | * @param array|object $key Document containing fields mapped to values,
|
269 | 268 | * which denote order or an index type
|
@@ -294,34 +293,16 @@ public function createIndex($key, array $options = array())
|
294 | 293 | *
|
295 | 294 | * @see http://docs.mongodb.org/manual/reference/command/createIndexes/
|
296 | 295 | * @see http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/
|
297 |
| - * @param array $indexes List of index specifications |
| 296 | + * @param array[] $indexes List of index specifications |
298 | 297 | * @return string[] The names of the created indexes
|
299 | 298 | * @throws InvalidArgumentException if an index specification is invalid
|
300 | 299 | */
|
301 | 300 | public function createIndexes(array $indexes)
|
302 | 301 | {
|
303 |
| - if (empty($indexes)) { |
304 |
| - return array(); |
305 |
| - } |
306 |
| - |
307 |
| - foreach ($indexes as $i => $index) { |
308 |
| - if ( ! is_array($index)) { |
309 |
| - throw new UnexpectedTypeException($index, 'array'); |
310 |
| - } |
311 |
| - |
312 |
| - if ( ! isset($index['ns'])) { |
313 |
| - $index['ns'] = $this->ns; |
314 |
| - } |
315 |
| - |
316 |
| - $indexes[$i] = new IndexInput($index); |
317 |
| - } |
318 |
| - |
319 |
| - $readPreference = new ReadPreference(ReadPreference::RP_PRIMARY); |
320 |
| - $server = $this->manager->selectServer($readPreference); |
| 302 | + $operation = new CreateIndexes($this->dbname, $this->collname, $indexes); |
| 303 | + $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY)); |
321 | 304 |
|
322 |
| - return (FeatureDetection::isSupported($server, FeatureDetection::API_CREATEINDEXES_CMD)) |
323 |
| - ? $this->createIndexesCommand($server, $indexes) |
324 |
| - : $this->createIndexesLegacy($server, $indexes); |
| 305 | + return $operation->execute($server); |
325 | 306 | }
|
326 | 307 |
|
327 | 308 | /**
|
@@ -1184,46 +1165,6 @@ protected function _update($filter, $update, $options)
|
1184 | 1165 | return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
|
1185 | 1166 | }
|
1186 | 1167 |
|
1187 |
| - /** |
1188 |
| - * Create one or more indexes for the collection using the createIndexes |
1189 |
| - * command. |
1190 |
| - * |
1191 |
| - * @param Server $server |
1192 |
| - * @param IndexInput[] $indexes |
1193 |
| - * @return string[] The names of the created indexes |
1194 |
| - */ |
1195 |
| - private function createIndexesCommand(Server $server, array $indexes) |
1196 |
| - { |
1197 |
| - $command = new Command(array( |
1198 |
| - 'createIndexes' => $this->collname, |
1199 |
| - 'indexes' => $indexes, |
1200 |
| - )); |
1201 |
| - $server->executeCommand($this->dbname, $command); |
1202 |
| - |
1203 |
| - return array_map(function(IndexInput $index) { return (string) $index; }, $indexes); |
1204 |
| - } |
1205 |
| - |
1206 |
| - /** |
1207 |
| - * Create one or more indexes for the collection by inserting into the |
1208 |
| - * "system.indexes" collection (MongoDB <2.6). |
1209 |
| - * |
1210 |
| - * @param Server $server |
1211 |
| - * @param IndexInput[] $indexes |
1212 |
| - * @return string[] The names of the created indexes |
1213 |
| - */ |
1214 |
| - private function createIndexesLegacy(Server $server, array $indexes) |
1215 |
| - { |
1216 |
| - $bulk = new BulkWrite(true); |
1217 |
| - |
1218 |
| - foreach ($indexes as $index) { |
1219 |
| - $bulk->insert($index); |
1220 |
| - } |
1221 |
| - |
1222 |
| - $server->executeBulkWrite($this->dbname . '.system.indexes', $bulk); |
1223 |
| - |
1224 |
| - return array_map(function(IndexInput $index) { return (string) $index; }, $indexes); |
1225 |
| - } |
1226 |
| - |
1227 | 1168 | /**
|
1228 | 1169 | * Returns information for all indexes for this collection using the
|
1229 | 1170 | * listIndexes command.
|
|
0 commit comments