'collection.dropIndex(index)' method is used to drop an index using collection handle.
Let’s see it with an example.
127.0.0.1:8529@abc_org> db.employees.getIndexes() [ { "fields" : [ "_key" ], "id" : "employees/0", "name" : "primary", "selectivityEstimate" : 1, "sparse" : false, "type" : "primary", "unique" : true }, { "deduplicate" : true, "fields" : [ "age" ], "id" : "employees/608", "name" : "idx_1699387292727115776", "selectivityEstimate" : 1, "sparse" : false, "type" : "persistent", "unique" : false }, { "deduplicate" : true, "fields" : [ "firstName", "lastName" ], "id" : "employees/648", "name" : "firstNameLastNameIndex", "selectivityEstimate" : 1, "sparse" : false, "type" : "persistent", "unique" : false } ]
As shown in the above snippet, employees collection has 3 indexes.
a. primary index on _key attribute
b. idx_1699387292727115776 index on name attribute
c. firstNameLastNameIndex index on firstName and lastName attributes.
You can’t drop some special indexes like primary and edge indexes. For example, let’s try to drop a primary index.
127.0.0.1:8529@abc_org> db.employees.dropIndex("primary") false
As you see the output, this method return false.
Let’s drop the index with name idx_1699387292727115776.
127.0.0.1:8529@abc_org> db.employees.dropIndex("idx_1699387292727115776") true
Let’s confirm by querying all the indexes.
127.0.0.1:8529@abc_org> db.employees.getIndexes() [ { "fields" : [ "_key" ], "id" : "employees/0", "name" : "primary", "selectivityEstimate" : 1, "sparse" : false, "type" : "primary", "unique" : true }, { "deduplicate" : true, "fields" : [ "firstName", "lastName" ], "id" : "employees/648", "name" : "firstNameLastNameIndex", "selectivityEstimate" : 1, "sparse" : false, "type" : "persistent", "unique" : false } ]
If the index does not exist, then false is returned. If the index existed and was dropped, then true is returned.
For example, let’s drop the index firstNameLastNameIndex using the index handle "employees/648".
127.0.0.1:8529@abc_org> db._dropIndex("employees/648")
true
Get all the indexes.
127.0.0.1:8529@abc_org> db.employees.getIndexes()
[
{
"fields" : [
"_key"
],
"id" : "employees/0",
"name" : "primary",
"selectivityEstimate" : 1,
"sparse" : false,
"type" : "primary",
"unique" : true
}
]
No comments:
Post a Comment