db.collection.dropIndex()

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.

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

参数:
  • index – Specifies either the name or the key of the index to drop. You must use the name of the index if you specified a name during the index creation.

The db.collection.dropIndex() method cannot drop the _id index. Use the db.collection.getIndexes() method to view all indexes on a collection.

Consider the following examples of the db.collection.dropIndex() method that assumes the following indexes on the collection pets:

> db.pets.getIndexes()
[
   {  "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"
   }
]
  • To drop the index on the field cat, you must use the index name catIdx:

    db.pets.dropIndex( 'catIdx' )
    
  • To drop the index on the fields cat and dog, you use either the index name cat_1_dog_-1 or the key { "cat" : 1, "dog" : -1 }:

    db.pets.dropIndex( 'cat_1_dog_-1' )
    
    db.pets.dropIndex( { cat : 1, dog : -1 } )
    

MongoDB Manual (Index)

关于

生态系统

格式

资源