MongoDB\Collection::listIndexes()
定义
参数
$options
: array指定所需选项的数组。
名称类型说明comment
混合
maxTimeMS
整型
处理游标操作的累计时间限制(以毫秒为单位)。MongoDB 最早会在中断点之后中止操作。
会话
与操作相关联的客户端会话。
1.3 版本中的新增功能。
Return Values
可遍历的 MongoDB\Model\IndexInfoIterator
,其中包含集合的每个索引的MongoDB\Model\IndexInfo
对象。
错误/异常
MongoDB\Exception\InvalidArgumentException
用于与参数或选项解析相关的错误。
MongoDB\ 驱动程序\Exception\RuntimeException 对于扩展级别的其他错误(例如连接错误)。
例子
以下示例列出了test
数据库中restaurants
collection的所有索引:
$collection = (new MongoDB\Client)->test->restaurants; foreach ($collection->listIndexes() as $index) { var_dump($index); }
而输出将类似如下所示:
object(MongoDB\Model\IndexInfo)#8 (4) { ["v"]=> int(1) ["key"]=> array(1) { ["_id"]=> int(1) } ["name"]=> string(4) "_id_" ["ns"]=> string(16) "test.restaurants" } object(MongoDB\Model\IndexInfo)#12 (4) { ["v"]=> int(1) ["key"]=> array(1) { ["cuisine"]=> float(-1) } ["name"]=> string(10) "cuisine_-1" ["ns"]=> string(16) "test.restaurants" } object(MongoDB\Model\IndexInfo)#8 (4) { ["v"]=> int(1) ["key"]=> array(1) { ["borough"]=> float(1) } ["name"]=> string(9) "borough_1" ["ns"]=> string(16) "test.restaurants" }
另请参阅
MongoDB 手册中的listIndexes命令参考
MongoDB 手册中的索引文档