定義
listIndexes指定されたコレクションのインデックスに関する情報(非表示のインデックスや現在ビルド中のインデックスを含む)を返します。返されるインデックス情報には、インデックスの作成に使用されたキーとオプションが含まれます。 オプションで、結果の最初のバッチするのバッチするサイズを設定できます。
Tip
In
mongosh, this command can also be run through thedb.collection.getIndexes()helper method.Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
このコマンドの構文は、次のとおりです。
db.runCommand ( { listIndexes: "<collection-name>", cursor: { batchSize: <int> }, comment: <any> } )
コマンドフィールド
このコマンドは、次のフィールドを使用します。
フィールド | タイプ | 説明 |
|---|---|---|
| string | コレクションの名前。 |
| integer | 任意。カーソルのバッチするサイズを指定します。後続の |
| any | 任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。
コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。
|
必要なアクセス権
If access control is enforced, the built-in read role provides the required privileges to run listIndexes for the collections in a database.
動作
MongoDB Search インデックス
listIndexes does not return information on MongoDB Search indexes. Instead, use $listSearchIndexes.
クライアントの切断
If the client that issued listIndexes disconnects before the operation completes, MongoDB marks listIndexes for termination using killOp.
レプリカセット ノードの状態の制限
To run on a replica set member, listIndexes operations require the member to be in PRIMARY or SECONDARY state. If the member is in another state, such as STARTUP2, the operation errors.
ワイルドカード インデックス
MongoDB 6.3、6.0.5、および 5.0.16 以降では、wildcardProjection フィールドには送信された形式でインデックス プロジェクションが格納されます。それ以前のバージョンでは、プロジェクションは正規化された形式で保存されている場合があります。
The server uses the index the same way, but you may notice a difference in the output of the listIndexes and db.collection.getIndexes() commands.
出力
listIndexes.cursorカーソルによって指定されたバッチ サイズで返される結果セット。バッチ出力内の各ドキュメントには以下のフィールドが含まれます。
フィールドタイプ説明id
integer
64 ビット整数。ゼロの場合、それ以上の情報のバッチはありません。ゼロ以外の場合、カーソル ID は、
getMoreコマンドで使用して次のインデックス情報のバッチを取得できます。ns
string
データベースとコレクションの名前は次の形式です。
<database-name>.<collection-name>firstBatch
ドキュメント
インデックス情報には、インデックスの作成に使用されるキーとオプションが含まれます。インデックス オプション hidden は、値が true の場合にのみ存在します。
必要に応じて追加の結果を検索するには、
getMoreを使用します。
例
データベース インデックスの一覧表示
この例では、カーソル バッチ サイズを指定せずに contacts コレクションのインデックスを一覧表示します。
1 db.runCommand ( 2 { 3 listIndexes: "contacts" 4 } 5 )
1 { 2 cursor: { 3 id: Long("0"), 4 ns: 'test.contacts', 5 firstBatch: [ 6 { v: 2, key: { _id: 1 }, name: '_id_', ns: 'test.contacts' }, 7 { v: 2, key: { a: 1 }, name: 'a_1', ns: 'test.contacts' } 8 ] 9 }, 10 ok: 1 11 }
結果バッチサイズを指定する
この例では、contacts コレクションのインデックスを一覧表示し、カーソル バッチ サイズを 1 に指定します。
1 db.runCommand ( 2 { 3 listIndexes: "contacts", cursor: { batchSize: 1 } 4 } 5 )
1 { 2 cursor: { 3 id: Long("4809221676960028307"), 4 ns: 'test.contacts', 5 firstBatch: [ { v: 2, key: { _id: 1 }, name: '_id_', ns: 'test.contacts' } ] 6 }, 7 ok: 1 8 }
追加の結果を検索する
この例では、getMore を使用して、contacts コレクションから追加の結果バッチを検索します。
1 db.runCommand( 2 { 3 getMore: Long("4809221676960028307"), collection: "contacts" 4 } 5 )
1 { 2 cursor: { 3 nextBatch: [ { v: 2, key: { a: 1 }, name: 'a_1', ns: 'test.contacts' } ], 4 id: Long("0"), 5 ns: 'test.contacts' 6 }, 7 ok: 1 8 }