MongoDB\Collection::listIndexes()
定义
参数
$options
: array一个数组,指定所需的选项。
名称类型描述注释mixedmaxTimeMS整数处理游标操作累积时间限制(毫秒)。MongoDB在最早的中断点处终止操作。
会话用于关联操作的客户端会话。
新版本1.3.
返回值
可遍历的MongoDB\Model\IndexInfoIterator
,其中包含每个索引的MongoDB\Model\IndexInfo
对象。
错误/异常
MongoDB\Exception\InvalidArgumentException
,与参数或选项解析相关的错误。
MongoDB\Driver\Exception\RuntimeException,用于扩展级别的其他错误(例如连接错误)。
示例
以下示例列出了测试数据库中 restaurants
集合的所有索引
$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" }
另请参阅
listIndexes 命令参考 MongoDB 手册
索引文档 MongoDB 手册
枚举集合 规范