public createIndexes ( string | null $databaseName, string $collectionName, array[] $indexes ) : array | ||
$databaseName | string | null | database name. |
$collectionName | string | collection name. |
$indexes | array[] | indexes specification. Each specification should be an array in format: optionName => value The main options are: - keys: array, column names with sort order, to be indexed. This option is mandatory. - unique: bool, whether to create unique index. - name: string, the name of the index, if not set it will be generated automatically. - background: bool, whether to bind index in the background. - sparse: bool, whether index should reference only documents with the specified field. See [[https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#options-for-all-index-types]] for the full list of options. |
Résultat | array | command document. |
public function createIndexes($databaseName, $collectionName, $indexes)
{
$normalizedIndexes = [];
foreach ($indexes as $index) {
if (!isset($index['key'])) {
throw new InvalidParamException('"key" is required for index specification');
}
$index['key'] = $this->buildSortFields($index['key']);
if (!isset($index['ns'])) {
if ($databaseName === null) {
$databaseName = $this->db->getDefaultDatabaseName();
}
$index['ns'] = $databaseName . '.' . $collectionName;
}
if (!isset($index['name'])) {
$index['name'] = $this->generateIndexName($index['key']);
}
$normalizedIndexes[] = $index;
}
return ['createIndexes' => $collectionName, 'indexes' => $normalizedIndexes];
}