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

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

dropIndexes

バージョン6.0で変更。

The dropIndexes command drops one or more indexes (except the index on the _id field and the last remaining shard key index, if one exists) from the specified collection.

Tip

In mongosh, this command can also be run through the db.collection.dropIndex() and db.collection.dropIndexes() helper methods..

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 はクラウドでの MongoDB 配置のための完全管理サービスです

注意

このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

このコマンドの構文は、次のとおりです。

db.runCommand(
{
dropIndexes: <string>,
index: <string|document|arrayofstrings>,
writeConcern: <document>, comment: <any>
}
)

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

フィールド
タイプ
説明

dropIndexes

文字列

削除するインデックスを持つコレクションの名前。

index

string またはドキュメントまたは string の配列

削除するインデックス。

  • _idインデックスと最後に残っているシャードキー インデックスを除くすべてのインデックスをコレクションから削除するには(存在する場合)、 "*"を指定します。

  • 単一のインデックスを削除するには、インデックス名、インデックス仕様ドキュメント(インデックスがテキストインデックスの場合を除く)、またはインデックス名の配列のいずれかを指定します。 テキストインデックスを削除するには、インデックス仕様ドキュメントの代わりにインデックス名を指定します。 このインデックスが最後に残っているシャードキー インデックスである場合、 dropIndexesはエラーを発生させます。

  • 複数のインデックスを削除するには、インデックス名の配列を指定します。

writeConcern

ドキュメント

任意。 コマンドの drop書込み保証( write concern ) を表すドキュメント。デフォルトの書込み保証を使用する場合は省略します。

comment

any

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

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

MongoDB6.0 dropIndexes以降では、それを使用して最後に残っているシャードキー互換インデックスを削除しようとすると、 によりエラーが発生します。 を"*" dropIndexesに渡すと、_id インデックスと最後に残っているシャードキー互換インデックス(存在する場合)を 除く すべてのインデックスが削除されます。

MongoDB5.2 以降では、別のインデックスでビルドが進行中の場合でも、dropIndexes を使用して同じコレクションの既存のインデックスを削除できます。以前のバージョンでは、インデックスのインデックス構築が進行中に別のインデックスを削除しようとするとBackgroundOperationInProgressForNamespace エラーが発生していました。

The dropIndexes operation only kills queries that are using the index being dropped. This may include queries considering the index as part of query planning.

dropIndexes obtains an exclusive lock on the specified collection for the duration of the operation. All subsequent operations on the collection must wait until dropIndexes releases the lock.

存在しないインデックスを含むインデックス名の配列がメソッドに渡された場合、指定されたインデックスはいずれも削除せずにエラーになります。

_idフィールドのデフォルト インデックスを削除することはできません。

テキストインデックスを削除するには、インデックス仕様ドキュメントの代わりにインデックス名を指定します。

If an index specified to dropIndexes is still building, dropIndexes attempts to stop the in-progress build. Stopping an index build has the same effect as dropping the built index.

For replica sets, run dropIndexes on the primary. The primary stops the index build and creates an associated "abortIndexBuild" oplog entry. Secondaries which replicate the "abortIndexBuild" oplog entry stop the in-progress index build and discard the build job. See Index Build Process for detailed documentation on the index build process.

currentOp操作またはcreateIndexesdb.collection.createIndexes() 操作に関連付けられたインデックスビルドを識別するには、 を使用します。例については、「アクティブなインデックス操作」を参照してください。

MongoDB では、クエリ プランナーからインデックスを非表示または再表示する機能が提供されます。 プランナーからインデックスを非表示にすることで、実際にインデックスを削除せずに、インデックスを削除した場合の潜在的な影響を評価できます。

評価後にユーザーがインデックスを削除する場合は、非表示のインデックスを削除できます。つまり、削除するために最初に再表示する必要はありません。

ただし、影響が負の場合、ユーザーは削除されたインデックスを再度作成する必要がある代わりに、インデックスを再表示できます。 また、インデックスは非表示になっている間完全に維持されているため、非表示が解除されたらすぐに使用できるようになります。

非表示インデックスの詳細については、「 非表示のインデックス 」を参照してください

  • _id以外のすべてのインデックス を削除するには、 index"*"を指定します。

    db.runCommand( { dropIndexes: "collection", index: "*" } )
  • 単一のインデックスを削除するには、削除するインデックスの名前を指定して コマンドを発行します。 たとえば、 age_1という名前のインデックスを削除するには、次のコマンドを使用します。

    db.runCommand( { dropIndexes: "collection", index: "age_1" })

    mongosh provides the helper methods db.collection.dropIndex() and db.collection.dropIndexes():

    db.collection.dropIndex("age_1");
  • 複数のインデックスを削除するには、以下のようにインデックス名の配列を指定して コマンドを発行します。

    db.runCommand( { dropIndexes: "collection", index: [ "age_1", "age_1_status_1" ] } )