Docs 菜单

MongoDB\Collection::listIndexes()

MongoDB\Collection::listIndexes()

返回此collection的所有索引的信息。

function listIndexes(array $options = []): MongoDB\Model\IndexInfoIterator
$options : array

指定所需选项的数组。

名称
类型
说明

comment

混合

允许用户指定任意注释,以帮助通过数据库分析器currentOp输出和日志跟踪操作。

自 MongoDB 4.4 起此选项可用,如果为旧服务器版本指定,则会在执行时导致异常。

1.13 版本中的新增功能

maxTimeMS

整型

处理游标操作的累计时间限制(以毫秒为单位)。MongoDB 最早会在中断点之后中止操作。

会话

与操作相关联的客户端会话。

1.3 版本中的新增功能

可遍历的 MongoDB\Model\IndexInfoIterator ,其中包含集合的每个索引的MongoDB\Model\IndexInfo对象。

MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。

MongoDB\ 驱动程序\Exception\RuntimeException 对于扩展级别的其他错误(例如连接错误)。

以下示例列出了test数据库中restaurantscollection的所有索引:

<?php
$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"
}