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

listIndexes(データベースコマンド)

listIndexes

指定されたコレクションのインデックスに関する情報(非表示のインデックスや現在ビルド中のインデックスを含む)を返します。返されるインデックス情報には、インデックスの作成に使用されたキーとオプションが含まれます。 オプションで、結果の最初のバッチするのバッチするサイズを設定できます。

Tip

In mongosh, this command can also be run through the db.collection.getIndexes() helper method.

Helper methods are convenient for mongosh users, 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>
}
)

このコマンドは、次のフィールドを使用します。

フィールド
タイプ
説明

listIndexes

string

コレクションの名前。

cursor.batchSize

integer

任意。カーソルのバッチするサイズを指定します。後続の getMore 操作のバッチ サイズを指定するには、mongosh で batchSize() を使用します。

comment

any

任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。

コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。

listIndexes コマンドに設定されたコメントは、listIndexes カーソルで実行される後続の getMore コマンドに継承されます。

If access control is enforced, the built-in read role provides the required privileges to run listIndexes for the collections in a database.

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 を使用します。

listIndexes.ok

コマンドの戻り値です。値が1の場合は成功を示します。

この例では、カーソル バッチ サイズを指定せずに contacts コレクションのインデックスを一覧表示します。

1db.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 に指定します。

1db.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 コレクションから追加の結果バッチを検索します。

1db.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}