On this page本页内容
dropIndexes¶The dropIndexes command drops one or more indexes (except the index on the _id field) from the specified collection.
The command has the following form:
The command takes the following fields:
| dropIndexes | String | The name of the collection whose indexes to drop. |
| index | string or document or array of strings | The index or indexes to drop.
|
| writeConcern | document | drop command. Omit to use the default write concern. |
comment |
any |
A comment can be any valid BSON type (string, integer, object, array, etc).
|
Changed in version 4.2.在版本4.2中更改。
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.
Prior to MongoDB 4.2, dropIndexes obtained an exclusive lock on the parent database, blocking all operations on the database and all its collections until the operation completed.
If the method is passed an array of index names that includes a non-existent index, the method errors without dropping any of the specified indexes.
_id Index¶You cannot drop the default index on the _id field.
To drop a text index, specify the index name instead of the index specification document.
New in version 4.4:If an index specified to dropIndexes is still building, dropIndexes attempts to abort the in-progress build. Aborting an index build has the same effect as dropping the built index. Prior to MongoDB 4.4, dropIndexes would return an error if the collection had any in-progress index builds.
For replica sets, run dropIndexes on the primary. The primary aborts the index build and creates an associated “abortIndexBuild” oplog entry. Secondaries which replicate the “abortIndexBuild” oplog entry abort the in-progress index build and discard the build job. See Index Build Process for detailed documentation on the index build process and the specific timing for terminating an in-progress index build.
The indexes specified to dropIndexes must be the entire set of in-progress builds associated to a single createIndexes or db.collection.createIndexes() operation. To drop a specific index out of a set of related in-progress builds, wait until the index builds complete and specify that index to dropIndexes.
For example, a createIndexes /
createIndexes() operation creates three indexes. Assuming all three index builds are in-progress, dropIndexes must specify all three indexes to abort the index builds.
Use currentOp to identify the index builds associated to a createIndexes / createIndexes() operation. See Active Indexing Operations for an example.
_id indexes , specify "*" for the index (See Indexes Named *).
age_1, use the following command:
The mongo shell provides the helper methods db.collection.dropIndex() and db.collection.dropIndexes():
See also参阅