定義
db.collection.dropIndex(index)MongoDB とドライバー
このページではmongoshメソッドについて説明します。 MongoDBドライバーで同等のメソッドを確認するには、 プログラミング言語の対応するページを参照してください。コレクションから指定されたインデックスを削除または排除します。
注意
_idフィールドのデフォルト インデックスを削除することはできません。You cannot specify
db.collection.dropIndex("*")to drop all non-_idindexes. Usedb.collection.dropIndexes()instead.
To get the index name or the index specification document for the
db.collection.dropIndex()method, use thedb.collection.getIndexes()method.
パラメーター
The db.collection.dropIndex() method takes the following parameter:
Parameter | タイプ | 説明 |
|---|---|---|
| 文字列またはドキュメント | 必須。削除するインデックスを指定します。インデックスは、インデックス名またはインデックス仕様ドキュメントによって指定できます。 テキスト インデックスを削除するには、インデックス名を指定します。
If an index specified to |
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
動作
MongoDB5.2 以降では、別のインデックスでビルドが進行中の場合でも、db.collection.dropIndex() を使用して同じコレクションの既存のインデックスを削除できます。以前のバージョンでは、インデックスのインデックス構築が進行中に別のインデックスを削除しようとするとBackgroundOperationInProgressForNamespace エラーが発生していました。
リソースのロック
db.collection.dropIndex() obtains an exclusive lock on the specified collection for the duration of the operation. All subsequent operations on the collection must wait until db.collection.dropIndex() releases the lock.
進行中のインデックスビルドの停止
If an index specified to db.collection.dropIndex() is still building, db.collection.dropIndex() attempts to stop the in-progress build. Stopping an index build has the same effect as dropping the built index.
For replica sets, run db.collection.dropIndex() 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 では、クエリ プランナーからインデックスを非表示または再表示する機能が提供されます。 プランナーからインデックスを非表示にすることで、実際にインデックスを削除せずに、インデックスを削除した場合の潜在的な影響を評価できます。
評価後にユーザーがインデックスを削除する場合は、非表示のインデックスを削除できます。つまり、削除するために最初に再表示する必要はありません。
ただし、影響が負の場合、ユーザーは削除されたインデックスを再度作成する必要がある代わりに、インデックスを再表示できます。 また、インデックスは非表示になっている間完全に維持されているため、非表示が解除されたらすぐに使用できるようになります。
非表示インデックスの詳細については、「 非表示のインデックス 」を参照してください。
例
petsコレクションで、catフィールドに降順のインデックスを作成します。
db.pets.createIndex( { cat: -1 }, { name: "catIdx" } )
petsコレクションのインデックスを表示するには、 getIndexes() メソッドを実行します。
db.pets.getIndexes()
[ { v: 2, key: { _id: 1 }, name: '_id_' }, { v: 2, key: { cat: -1 }, name: 'catIdx' } ]
フィールドcat の単一フィールドインデックスには、ユーザー指定の名前の catIdx とインデックス仕様ドキュメントの { "cat" : -1 } があります。
インデックス catIdx を削除するには、次のいずれかのインデックス名を使用できます。
db.pets.dropIndex( "catIdx" )
または、インデックス仕様ドキュメント { "cat" : -1 } を使用できます。
db.pets.dropIndex( { "cat" : -1 } )
dropIndex コマンドは、コマンドが実行される前にコレクション内のインデックスの数を返し、コマンドが成功したかどうかを示します。
{ nIndexesWas: 2, ok: 1 }
インデックスが削除されたことを確認するには、getIndexes() メソッドを再度実行します。
db.pets.getIndexes()
[ { v: 2, key: { _id: 1 }, name: '_id_' } ]