Skip to content

Commit 6a17ac9

Browse files
committed
PHPLIB-69: Index drop methods
1 parent 7b6b8e0 commit 6a17ac9

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/Collection.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use MongoDB\Driver\ReadPreference;
1010
use MongoDB\Driver\BulkWrite;
1111
use MongoDB\Driver\WriteConcern;
12+
use InvalidArgumentException;
1213

1314
class Collection
1415
{
@@ -354,11 +355,21 @@ public function drop()
354355
* @see http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/
355356
* @param string $indexName
356357
* @return Cursor
357-
* @throws InvalidArgumentException if "*" is specified
358+
* @throws InvalidArgumentException if "*" is specified, since dropIndexes()
359+
* should be used to drop multiple indexes
358360
*/
359361
public function dropIndex($indexName)
360362
{
361-
// TODO
363+
$indexName = (string) $indexName;
364+
365+
if ($indexName === '*') {
366+
throw new InvalidArgumentException('dropIndexes() must be used to drop multiple indexes');
367+
}
368+
369+
$command = new Command(array('dropIndexes' => $this->collname, 'index' => $indexName));
370+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
371+
372+
return $this->manager->executeCommand($this->dbname, $command, $readPreference);
362373
}
363374

364375
/**
@@ -370,7 +381,10 @@ public function dropIndex($indexName)
370381
*/
371382
public function dropIndexes()
372383
{
373-
// TODO
384+
$command = new Command(array('dropIndexes' => $this->collname, 'index' => '*'));
385+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
386+
387+
return $this->manager->executeCommand($this->dbname, $command, $readPreference);
374388
}
375389

376390
/**

0 commit comments

Comments
 (0)