MongoDB とドライバー
定義
db.collection.dropIndex(index)コレクションから指定されたインデックスを削除または排除します。
注意
_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: 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 のバージョン
動作
Starting in MongoDB 8.3, db.collection.dropIndex() is idempotent. If you specify an index that does not exist, db.collection.dropIndex() ignores the non-existent index and completes successfully instead of returning an IndexNotFound error. In earlier versions, db.collection.dropIndex() returns an IndexNotFound error if the specified index does not exist.
You can use db.collection.dropIndex() to drop existing indexes on the same collection even if there is a build in progress on another index. In earlier versions, attempting to drop a different index during an in-progress index build results in a BackgroundOperationInProgressForNamespace error.
リソースのロック
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_' } ]