AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

MongoDB\Collection::createIndex()

MongoDB\Collection::createIndex()

コレクションの インデックス を作成します。

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

インデックスを作成するフィールドとインデックスの順序を指定します。

たとえば、次の例では、 usernameフィールドに降順のインデックスを指定しています。

[ 'username' => -1 ]
$options : 配列

必要なオプションを指定する配列。

$optionsパラメータは、インデックス オプションコマンド オプションの両方を受け入れます。 インデックス オプションの非網羅的なリストは次のとおりです。 インデックス オプションの完全なリストについては、MongoDB マニュアルのcreateIndexesコマンドのリファレンスを参照してください。

インデックス オプション(非排他的)

collation
array|object
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.

expireAfterSeconds

integer

TTLインデックスを作成します。

name

string

インデックスを一意に識別する名前。 デフォルトでは、MongoDB は キーに基づいてインデックス名を作成します。

部分フィルター式

array|object

部分インデックスを作成します。

sparse

ブール値

スパースインデックスを作成します。

unique

ブール値

一意のインデックスを作成します。

コマンド オプション

名前
タイプ
説明

comment

混合

データベースプロファイラcurrentOp出力、およびログから操作を追跡するのに役立つ任意のコメントを指定できるようにします。

このオプションは MongoDB 4.4 以降で使用可能であり、古いサーバー バージョンで指定すると実行時に例外が発生します。

バージョン1.13の新機能。

commitQuorum

string|integer

プライマリがインデックスを準備完了とマークする前に、プライマリがインデックス ビルドを正常に完了する必要があるレプリカセットのデータを持つノードの数を指定します。

このオプションは、書込み保証(write concern)のwフィールドと"votingMembers"の同じ値を受け入れます。これは、投票権を持つデータを保持するすべてのノードを示します。

これは 4.4 より前のサーバー バージョンではサポートされていないため、使用された場合は実行時に例外が発生します。

バージョン1.7の新機能。

maxTimeMS

integer

カーソルに対する情報処理操作の累積時間制限(ミリ秒単位)。MongoDB は、割り込みポイントの後の最も早く操作を中止します。

バージョン1.3の新機能。

セッション

操作に関連付けるクライアント セッション。

バージョン1.3の新機能。

writeConcern

操作に使用する書込み保証 ( write concern )。 コレクションの書込み保証 (write concern) のデフォルトです。

トランザクションの一部である個々の操作に対して書込み保証 (write concern)を指定することはできません。代わりに、トランザクションを開始するときにwriteConcern オプションを設定します。

作成されたインデックスの名前を string として表します。

MongoDB\Exception\UnsupportedExceptionオプションが使用され、選択したサーバーでサポートされていない場合(例: collationreadConcernwriteConcern )。

MongoDB\Exception\InvalidArgumentException は、パラメータまたはオプションの解析に関連するエラーの場合は です。

MongoDB$Driver\Exception\RuntimeException は、拡張レベルの他のエラーの場合(例:)。

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

出力は次のようになります。

string(19) "borough_1_cuisine_1"

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

出力は次のようになります。

string(9) "borough_1"