diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 6575021f7..53c0393aa 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -80,19 +80,24 @@ public function dropIndex($columns = null) { $columns = $this->fluent($columns); - // Columns are passed as a default array. - if (is_array($columns) && is_int(key($columns))) { - // Transform the columns to the required array format. - $transform = []; + $listOfCurrentIndexes = []; + foreach ($this->collection->listIndexes() as $currentIndexName) { + $listOfCurrentIndexes[] = $currentIndexName['name']; + } - foreach ($columns as $column) { - $transform[$column] = $column . '_1'; + //Filter columns with exists indexes + $existsIndexes = []; + foreach ($columns as $indexName) { + if (in_array($indexName, $listOfCurrentIndexes)) { + $existsIndexes[] = $indexName; + } + $postFixedIndexName = $indexName . '_1'; + if (in_array($postFixedIndexName, $listOfCurrentIndexes)) { + $existsIndexes[] = $postFixedIndexName; } - - $columns = $transform; } - foreach ($columns as $column) { + foreach ($existsIndexes as $column) { $this->collection->dropIndex($column); } diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index d2cc86cf4..307118e79 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -106,6 +106,17 @@ public function testDropIndex() $index = $this->getIndex('newcollection', 'uniquekey'); $this->assertEquals(null, $index); + + Schema::collection('newcollection', function ($collection) { + $collection->index( + 'uniquekey', + 'uniquekey' + ); + $collection->dropIndex(['uniquekey']); + }); + + $index = $this->getIndex('newcollection', 'uniquekey'); + $this->assertEquals(null, $index); } public function testBackground()