Commit 0536d51b authored by Katherine Walker's avatar Katherine Walker

PHPLIB-84: Allow Collection::dropIndex() to accept an IndexInfo object

parent 857cfc84
arg_name: param
name: $indexName
type: string
type: string| :phpclass:`MongoDB\\Model\\IndexInfo`
description: |
The name of the index to drop. View the existing indexes on the collection
using the :phpmethod:`listIndexes() <MongoDB\\Collection::listIndexes>`
method.
The name or model object of the index to drop. View the existing indexes on
the collection using the :phpmethod:`listIndexes()
<MongoDB\\Collection::listIndexes>` method.
interface: phpmethod
operation: ~
optional: false
......
......@@ -29,6 +29,7 @@ use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Model\IndexInfo;
use MongoDB\Model\IndexInfoIterator;
use MongoDB\Operation\Aggregate;
use MongoDB\Operation\BulkWrite;
......@@ -443,7 +444,7 @@ class Collection
* Drop a single index in the collection.
*
* @see DropIndexes::__construct() for supported options
* @param string $indexName Index name
* @param string|IndexInfo $indexName Index name or model object
* @param array $options Additional options
* @return array|object Command result document
* @throws UnsupportedException if options are not supported by the selected server
......
......@@ -60,6 +60,16 @@ class IndexInfo implements ArrayAccess
return $this->info;
}
/**
* Return the index name to allow casting IndexInfo to string.
*
* @return string
*/
public function __toString()
{
return $this->getName();
}
/**
* Return the index key.
*
......
......@@ -2,6 +2,7 @@
namespace MongoDB\Tests\Operation;
use MongoDB\Model\IndexInfo;
use MongoDB\Operation\CreateIndexes;
use MongoDB\Operation\DropIndexes;
use MongoDB\Operation\ListIndexes;
......@@ -88,6 +89,34 @@ class DropIndexesFunctionalTest extends FunctionalTestCase
}
}
public function testDropByIndexInfo()
{
$info = new IndexInfo([
'v' => 1,
'key' => ['x' => 1],
'name' => 'x_1',
'ns' => 'foo.bar',
]);
$operation = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), [['key' => ['x' => 1]]]);
$createdIndexNames = $operation->execute($this->getPrimaryServer());
$this->assertSame('x_1', $createdIndexNames[0]);
$this->assertIndexExists('x_1');
$operation = new DropIndexes($this->getDatabaseName(), $this->getCollectionName(), $info);
$this->assertCommandSucceeded($operation->execute($this->getPrimaryServer()));
$operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
$indexes = $operation->execute($this->getPrimaryServer());
foreach ($indexes as $index) {
if ($index->getName() === 'x_1') {
$this->fail('The "x_1" index should have been deleted');
}
}
}
public function testSessionOption()
{
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment