Navigation
This version of the documentation is archived and no longer supported. To learn how to upgrade your version of PHP Library Manual, refer to the upgrade documentation.

Indexes

Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.

The PHP driver supports managing indexes through the MongoDB\Collection class, which implements MongoDB’s cross-driver Index Management and Enumerating Indexes specifications.

This document provides an introduction to creating, listing, and dropping indexes using the MongoDB PHP Library. The MongoDB Manual’s Indexes reference provides more thorough information about indexing in MongoDB.

Create Indexes

Create indexes with the MongoDB\Collection::createIndex() or MongoDB\Collection::createIndexes() methods. Refer to the method reference for more details about each method.

The following example creates an ascending index on the state field using the createIndex() method:

<?php

$collection = (new MongoDB\Client)->test->zips;

$result = $collection->createIndex(['state' => 1]);

var_dump($result);

When you create an index, the method returns its name, which is automatically generated from its specification. The above example would output something similar to:

string(7) "state_1"

List Indexes

The MongoDB\Collection::listIndexes() method provides information about the indexes in a collection. The MongoDB\Collection::listIndexes() method returns an iterator of MongoDB\Model\IndexInfo objects, which you can use to view information about each index. Refer to the method reference for more details.

The following example lists all indexes in the zips collection in the test database:

<?php

$collection = (new MongoDB\Client)->test->zips;

foreach ($collection->listIndexes() as $indexInfo) {
    var_dump($indexInfo);
}

The output would resemble:

object(MongoDB\Model\IndexInfo)#10 (4) {
  ["v"]=>
  int(1)
  ["key"]=>
  array(1) {
    ["_id"]=>
    int(1)
  }
  ["name"]=>
  string(4) "_id_"
  ["ns"]=>
  string(9) "test.zips"
}
object(MongoDB\Model\IndexInfo)#13 (4) {
  ["v"]=>
  int(1)
  ["key"]=>
  array(1) {
    ["state"]=>
    int(1)
  }
  ["name"]=>
  string(7) "state_1"
  ["ns"]=>
  string(9) "test.zips"
}

Drop Indexes

The MongoDB\Collection::dropIndex() method lets you drop a single index while MongoDB\Collection::dropIndexes() drops all of the indexes on a collection. Refer to the method reference for more details about each method.

The following example drops a single index by its name, state_1:

<?php

$collection = (new MongoDB\Client)->test->zips;

$result = $collection->dropIndex('state_1');

var_dump($result);

The operation’s output would resemble:

object(MongoDB\Model\BSONDocument)#11 (1) {
  ["storage":"ArrayObject":private]=>
  array(2) {
    ["nIndexesWas"]=>
    int(2)
    ["ok"]=>
    float(1)
  }
}
←   GridFS Example Data  →