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::createIndexes()

Definition

MongoDB\Collection::createIndexes

Create one or more indexes for the collection.

function createIndexes(array $indexes, array $options = []): string[]

This method has the following parameters:

Parameter Type Description
$indexes array

The indexes to create on the collection.

For example, the following specifies a unique index on the username field and a compound index on the email and createdAt fields:

[
    [ 'key' => [ 'username' => -1 ], 'unique' => true ],
    [ 'key' => [ 'email' => 1, 'createdAt' => 1 ] ],
]
$options array Optional. An array specifying the desired options.

The $options parameter supports the following options:

Option Type Description
commitQuorum string|integer

Optional. Specifies how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready.

This option accepts the same values for the w field in a write concern plus "votingMembers", which indicates all voting data-bearing nodes.

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

New in version 1.7.

maxTimeMS integer

Optional. The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.

New in version 1.3.

session MongoDB\Driver\Session

Optional. Client session to associate with the operation.

Sessions are not supported for server versions prior to 3.6.

New in version 1.3.

writeConcern MongoDB\Driver\WriteConcern

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

It is not possible to specify a write concern for individual operations as part of a transaction. Instead, set the writeConcern option when starting the transaction with startTransaction.

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

Return Values

The names of the created indexes as an array of strings.

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).

$indexes parameter

The $indexes parameter is an array of index specification documents. Each element in $indexes must itself be an array or object with a key field, which corresponds to the $key parameter of createIndex(). The array or object may include other fields that correspond to index options accepted by createIndex() (excluding writeConcern).

For example, the following $indexes parameter creates two indexes. The first is an ascending unique index on the username field and the second is a 2dsphere index on the loc field with a custom name:

[
    [ 'key' => [ 'username' => 1 ], 'unique' => true ],
    [ 'key' => [ 'loc' => '2dsphere' ], 'name' => 'geo_index' ],
]

Example

The following example creates two indexes on the restaurants collection in the test database. One index is a compound index on the borough and cuisine fields and the other is 2dsphere index on the loc field with a custom name.

<?php

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

$indexNames = $collection->createIndexes([
    [ 'key' => [ 'borough' => 1, 'cuisine' => 1] ],
    [ 'key' => [ 'loc' => '2dsphere'], 'name' => 'geo_index' ],
]);

var_dump($indexNames);

The output would then resemble:

array(2) {
  [0]=>
  string(19) "borough_1_cuisine_1"
  [1]=>
  string(9) "geo_index"
}

See Also