db.collection.dropIndex()
在本页面
db.collection.dropIndex
(索引)- 从集合中删除或删除指定的索引。 db.collection.dropIndex()方法在dropIndexes命令周围提供包装。注意您不能删除
_id
字段上的默认索引。从MongoDB 4.2开始,您不能指定db.collection.dropIndex("*")
删除所有非_id
索引。使用db.collection.dropIndexes()
代替。
db.collection.dropIndex()方法采用以下参数:
参数 | 类型 | 描述 |
---|---|---|
index | string or document | 指定要删除的索引。您可以通过索引 name 或索引规范文档指定索引。 [1] 要删除文本索引,请指定索引 name。 从MongoDB 4.2开始,您不能指定 "*" 删除所有非_id 索引。使用 db.collection.dropIndexes() 代替。 |
要获取db.collection.dropIndex()方法的索引 name 或索引规范文档,请使用db.collection.getIndexes()方法。
警告此命令在受影响的数据库上获取写锁定,并将阻止其他操作,直到完成为止。
从MongoDB 4.2开始,该
dropIndex()
操作只会终止使用正在删除的索引的查询。这可能包括将索引视为查询计划一部分的 查询。在MongoDB 4.2之前,在集合上删除索引将杀死该集合上所有打开的查询。
在版本4.2中进行了更改。
db.collection.dropIndex()
在操作期间获得对指定集合的排他锁。集合上的所有后续操作都必须等到db.collection.dropIndex()
释放锁为止。在MongoDB 4.2之前的版本中,
db.collection.dropIndex()
获得了对父数据库的排他锁,阻止了对数据库_及其_所有集合的所有操作,直到操作完成。考虑一个
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“
}
]
要删除索引
catIdx
,可以使用索引 name:db.pets.dropIndex( “catIdx“ )
或者您可以使用索引规范文档
{ “cat“ : -1 }