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

db.コレクション.dropIndex()(mongoshメソッド)

db.collection.dropIndex(index)

MongoDB とドライバー

このページではmongoshメソッドについて説明します。 MongoDBドライバーで同等のメソッドを確認するには、 プログラミング言語の対応するページを参照してください。

コレクションから指定されたインデックスを削除または排除します。

注意

To get the index name or the index specification document for the db.collection.dropIndex() method, use the db.collection.getIndexes() method.

The db.collection.dropIndex() method takes the following parameter:

Parameter
タイプ
説明

index

文字列またはドキュメント

必須。削除するインデックスを指定します。インデックスは、インデックス名またはインデックス仕様ドキュメントによって指定できます。

テキスト インデックスを削除するには、インデックス名を指定します。

"*"を指定して_id以外のインデックスをすべて削除することはできません。 代わりにdb.collection.dropIndexes()を使用してください。

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. See Stop In-Progress Index Builds for more complete documentation.

このメソッドは、次の環境でホストされている配置で使用できます。

  • 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() 操作に関連付けられたインデックスビルドを識別するには、 を使用します。例については、「アクティブなインデックス操作」を参照してください。

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_' } ]
このページを評価