MongoDB – Drop Index:
In MongoDB, we can remove the index by using the command below. We need to provide the required fields on which the indexes need to be removed.
Syntax:
db.collection.dropIndex(fields)
First verify the indexes already created in a collection.
testset:PRIMARY>db.students.getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.students"
},
{
"v" : 1,
"key" : {
"firstname" : 1
},
"name" : "firstname_1",
"ns" : "test.students"
},
{
"v" : 1,
"key" : {
"firstname" : 1,
"city" : 1
},
"name" : "firstname_1_city_1",
"ns" : "test.students"
}
]
Drop the index on “firstname” field.
testset:PRIMARY>db.students.dropIndex({firstname:1})
{ "nIndexesWas" : 3, "ok" : 1 }
Now Verify the index that was removed
testset:PRIMARY>db.students.getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.students"
},
{
"v" : 1,
"key" : {
"firstname" : 1,
"city" : 1
},
"name" : "firstname_1_city_1",
"ns" : "test.students"
}
]
