OPTIONS
翻译或纠错本页面

db.collection.dropIndex()

Definition

db.collection.dropIndex(index)

Drops or removes the specified index from a collection. The db.collection.dropIndex() method provides a wrapper around the dropIndexes command.

注解

You cannot drop the default index on the _id field.

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

Parameter Type Description
index string or document

Specifies the index to drop. You can specify the index either by the index name or by the index specification document. [1]

To drop a text index, specify the index name.

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

Example

Consider a pets collection. Calling the getIndexes() method on the pets collection returns the following indexes:

[
   {  "v" : 1,
      "key" : { "_id" : 1 },
      "ns" : "test.pets",
      "name" : "_id_"
   },
   {
      "v" : 1,
      "key" : { "cat" : -1 },
      "ns" : "test.pets",
      "name" : "catIdx"
   },
   {
      "v" : 1,
      "key" : { "cat" : 1, "dog" : -1 },
      "ns" : "test.pets",
      "name" : "cat_1_dog_-1"
   }
]

The single field index on the field cat has the user-specified name of catIdx [2] and the index specification document of { "cat" : -1 }.

To drop the index catIdx, you can use either the index name:

db.pets.dropIndex( "catIdx" )

Or you can use the index specification document { "cat" : -1 }:

db.pets.dropIndex( { "cat" : -1 } )
[1](1, 2) When using a mongo shell version earlier than 2.2.2, if you specified a name during the index creation, you must use the name to drop the index.
[2]During index creation, if the user does not specify an index name, the system generates the name by concatenating the index key field and value with an underscore, e.g. cat_1.