MongoDB\Collection::dropIndexes()
定义
参数
$indexName
: string|MongoDB\Model\IndexInfo
- 要删除的索引的名称或模型对象。通过使用
MongoDB\Collection::listIndexes()
方法查看集合上的现有索引。 $options
: array一个指定所需选项的数组。
返回值
一个数组或对象,包含dropIndexes命令的结果文档。返回类型将取决于typeMap
选项。
错误/异常
MongoDB\Exception\UnsupportedException
如果使用了所选服务器不支持(例如 collation
、readConcern
、writeConcern
)的选项。
MongoDB\Exception\InvalidArgumentException
对于与参数或选项解析相关的错误。
MongoDB\Driver\Exception\RuntimeException 对于扩展级别的其他错误(例如连接错误)。
示例
以下示例将删除 test
数据库中 restaurants
集合的所有索引
$collection = (new MongoDB\Client)->test->restaurants; $result = $collection->dropIndexes(); var_dump($result);
输出结果将类似
object(MongoDB\Model\BSONDocument)#9 (1) { ["storage":"ArrayObject":private]=> array(3) { ["nIndexesWas"]=> int(3) ["msg"]=> string(38) "non-_id indexes dropped for collection" ["ok"]=> float(1) } }
另请参阅
dropIndexes 命令参考 MongoDB 手册
索引文档 MongoDB 手册