db.collection.dropIndex()

在本页面

Definition

Note

您不能将默认索引放在_id字段上。

db.collection.dropIndex()方法采用以下参数:

Parameter Type Description
index 字符串或文件 指定要删除的索引。您可以通过索引名称或索引规范文档指定索引。 [1]


要删除text索引,请指定索引名称。

要获取db.collection.dropIndex()方法的索引名称或索引规范文档,请使用db.collection.getIndexes()方法。

Warning

此命令在受影响的数据库上获得写锁定,并将阻止其他操作,直到完成为止。

Example

考虑一个pets集合。在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"
   }
]

字段cat上的单个字段索引具有用户指定的名称catIdx [2]和索引指定文档{ "cat" : -1 }

要删除索引catIdx,可以使用以下索引名称之一:

db.pets.dropIndex( "catIdx" )

或者,您可以使用索引规范文档{ "cat" : -1 }

db.pets.dropIndex( { "cat" : -1 } )
[1] 使用低于 2.2.2 的mongo shell 版本时,如果在创建索引的过程中指定了名称,则必须使用该名称删除索引。
[2] 在创建索引期间,如果用户未指定索引名称,则系统会通过将索引键字段和值与下划线(例如下划线)连接起来来生成名称。 cat_1
首页