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.

MongoDB\Collection::createIndex()

Definition

MongoDB\Collection::createIndex

Create an index for the collection.

function createIndex($key, array $options = []): string

This method has the following parameters:

Parameter Type Description
$key array|object

Specifies the field or fields to index and the index order.

For example, the following specifies a descending index on the username field:

[ 'username' => -1 ]
$options array Optional. An array specifying the desired options.

The $options parameter accepts all index options that your MongoDB version supports. MongoDB includes the following options:

Option Type Description
unique boolean Optional. Creates a unique index.
collation array|object

Optional. Specifies the collation for the index.

Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, the locale field is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.

If the collation is unspecified but the collection has a default collation, the operation uses the collation specified for the collection. If no collation is specified for the collection or for the operation, MongoDB uses the simple binary comparison used in prior versions for string comparisons.

This option is available in MongoDB 3.4+ and will result in an exception at execution time if specified for an older server version.

partialFilterExpression array|object Optional. Creates a partial index.
sparse boolean Optional. Creates a sparse index.
expireAfterSeconds integer Optional. Creates a TTL index.
name string Optional. A name that uniquely identifies the index. By default, MongoDB creates index names based on the key.
background string Optional. Instructs MongoDB to build the index as a background process.
2dsphereIndexVersion integer

Optional. Specifies the version of a 2dsphere index to create.

MongoDB 2.6 introduced version 2 of 2dsphere indexes. Version 2 is the default version of 2dsphere indexes created in MongoDB 2.6 and later versions. 2dsphereIndexVersion enables you to override the default version 2.

writeConcern MongoDB\Driver\WriteConcern

Optional. Write concern to use for the operation. Defaults to the collection’s write concern.

This is not supported for server versions prior to 3.4 and will result in an exception at execution time if used.

For a full list of the supported index creation options, refer to the createIndexes command reference in the MongoDB manual.

Return Values

The name of the created index as a string.

Errors/Exceptions

MongoDB\Exception\UnsupportedException if options are used and not supported by the selected server (e.g. collation, readConcern, writeConcern).

MongoDB\Exception\InvalidArgumentException for errors related to the parsing of parameters or options.

MongoDB\Driver\Exception\RuntimeException for other errors at the driver level (e.g. connection errors).

Examples

Create a Compound Index

The following example creates a compound index on the borough and cuisine fields in the restaurants collection in the test database.

<?php

$collection = (new MongoDB\Client)->selectCollection('test', 'restaurants');

$indexName = $collection->createIndex(['borough' => 1, 'cuisine' => 1]);

var_dump($indexName);

The output would then resemble:

string(19) "borough_1_cuisine_1"

Create a Partial Index

The following example adds a partial index on the borough field in the restaurants collection in the test database. The partial index indexes only documents where the borough field exists.

<?php

$collection = (new MongoDB\Client)->selectCollection('test', 'restaurants');

$indexName = $collection->createIndex(
   ['borough' => 1],
   [
       'partialFilterExpression' => [
           'borough' => ['$exists' => true],
       ],
   ]
);

var_dump($indexName);

The output would then resemble:

string(9) "borough_1"

See Also