定義
dropIndexesバージョン6.0で変更。
The
dropIndexescommand drops one or more indexes (except the index on the_idfield and the last remaining shard key index, if one exists) from the specified collection.Tip
In
mongosh, this command can also be run through thedb.collection.dropIndex()anddb.collection.dropIndexes()helper methods..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 はクラウドでの 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 の配列 | 削除するインデックス。
|
writeConcern | ドキュメント | 任意。 コマンドの |
| 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 Index
_idフィールドのデフォルト インデックスを削除することはできません。
text Indexes
テキストインデックスを削除するには、インデックス仕様ドキュメントの代わりにインデックス名を指定します。
進行中のインデックスビルドの停止
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() 操作に関連付けられたインデックスビルドを識別するには、 を使用します。例については、「アクティブなインデックス操作」を参照してください。
Hidden Indexes
MongoDB では、クエリ プランナーからインデックスを非表示または再表示する機能が提供されます。 プランナーからインデックスを非表示にすることで、実際にインデックスを削除せずに、インデックスを削除した場合の潜在的な影響を評価できます。
評価後にユーザーがインデックスを削除する場合は、非表示のインデックスを削除できます。つまり、削除するために最初に再表示する必要はありません。
ただし、影響が負の場合、ユーザーは削除されたインデックスを再度作成する必要がある代わりに、インデックスを再表示できます。 また、インデックスは非表示になっている間完全に維持されているため、非表示が解除されたらすぐに使用できるようになります。
非表示インデックスの詳細については、「 非表示のインデックス 」を参照してください。
例
_id以外のすべてのインデックス を削除するには、indexの"*"を指定します。db.runCommand( { dropIndexes: "collection", index: "*" } ) 単一のインデックスを削除するには、削除するインデックスの名前を指定して コマンドを発行します。 たとえば、
age_1という名前のインデックスを削除するには、次のコマンドを使用します。db.runCommand( { dropIndexes: "collection", index: "age_1" }) mongoshprovides the helper methodsdb.collection.dropIndex()anddb.collection.dropIndexes():db.collection.dropIndex("age_1"); 複数のインデックスを削除するには、以下のようにインデックス名の配列を指定して コマンドを発行します。
db.runCommand( { dropIndexes: "collection", index: [ "age_1", "age_1_status_1" ] } )