定義
パラメーター
$key: array|objectインデックスを作成するフィールドとインデックスの順序を指定します。
たとえば、次の例では、
usernameフィールドに降順のインデックスを指定しています。[ 'username' => -1 ] $options: 配列必要なオプションを指定する配列。
$optionsパラメータは、インデックス オプションとコマンド オプションの両方を受け入れます。 インデックス オプションの非網羅的なリストは次のとおりです。 インデックス オプションの完全なリストについては、MongoDB マニュアルのcreateIndexesコマンドのリファレンスを参照してください。インデックス オプション(非排他的)
collationarray|objectCollation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, thelocalefield 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オプションを設定します。
Return Values
作成されたインデックスの名前を string として表します。
エラーと例外
MongoDB\Exception\UnsupportedExceptionオプションが使用され、選択したサーバーでサポートされていない場合(例: collation 、 readConcern 、 writeConcern )。
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.
$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.
$collection = (new MongoDB\Client)->selectCollection('test', 'restaurants'); $indexName = $collection->createIndex( ['borough' => 1], [ 'partialFilterExpression' => [ 'borough' => ['$exists' => true], ], ] ); var_dump($indexName);
出力は次のようになります。
string(9) "borough_1"
その他の参照
MongoDB マニュアルのcreateIndexesコマンドに関する参考資料
MongoDB マニュアルのインデックスに関するドキュメント