MongoDB\Collection::listIndexes()
Definition
Parameters
$options
: arrayAn array specifying the desired options.
NameTypeDescriptioncomment
mixed
Enables users to specify an arbitrary comment to help trace the operation through the database profiler, currentOp output, and logs.
To use this option, you must connect to MongoDB 6.0 or later. If you are connected to an earlier version, the server returns an exception at execution time.
New in version 1.13.
maxTimeMS
integer
The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.
session
Client session to associate with the operation.
New in version 1.3.
Return Values
An Iterator
instance, which provides a MongoDB\Model\IndexInfo
object
for each index for the collection.
Errors/Exceptions
MongoDB\Exception\InvalidArgumentException
for errors related to
the parsing of parameters or options.
MongoDB\Driver\Exception\RuntimeException for other errors at the extension level (e.g. connection errors).
Example
The following example lists all of the indexes for the restaurants
collection in the test
database:
$collection = (new MongoDB\Client)->test->restaurants; foreach ($collection->listIndexes() as $index) { var_dump($index); }
The output would then resemble:
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" }
See Also
listIndexes command reference in the MongoDB manual
Index documentation in the MongoDB manual
Index Management specification