使用命令列工具,如何從 MongoDB 資料庫中的所有集合中刪除所有索引?
以下是使用命令列從 MongoDB 資料庫中的所有集合中刪除所有索引的語法
db.getCollectionNames().forEach(function(yourVariableName) {
db.runCommand({dropIndexes: yourVariableName, index: "*"});
});上述語法將刪除除了_id外的所有索引。
檢查當前資料庫。以下是查詢
> db
這將生成以下輸出
Test
以下是刪除索引之前從集合中顯示某些索引的查詢
> db.indexingDemo.getIndexes();
這將生成以下輸出
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.indexingDemo"
},
{
"v" : 2,
"key" : {
"StudentFavouriteSubject" : 1
},
"name" : "StudentFavouriteSubject_1",
"ns" : "test.indexingDemo",
"background" : true
}
]以下是刪除 MongoDB 資料庫中所有集合的所有索引的查詢
> db.getCollectionNames().forEach(function(allCollectionName) {
... db.runCommand({dropIndexes: allCollectionName, index: "*"});
... });以下是檢查是否已刪除索引的查詢
> db.indexingDemo.getIndexes();
這將生成以下輸出
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.indexingDemo"
}
]檢視以上樣本輸出,已成功刪除索引。
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP