Managing existing indexes
In this recipe, we will be looking at some common operations we can perform on indexes like viewing, deleting, checking index sizes, and re-indexing.
Getting ready
For this recipe, load the sample dataset and create an index on the city
field, as described in the previous recipe.
How to do it...
- We begin by connecting to the mongo shell of the server and viewing all indexes on the system:
> db.mockdata.getIndexes()
The following result is obtained:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mydb.mockdata" }, { "v" : 2, "key" : { "city" : 1, "first_name" : 1 }, "name" : "city_1_first_name_1", "ns" : "mydb.mockdata" } ]
- Execute a
dropIndex()
command to delete a particular index:
> db.mockdata.dropIndex('city_1_first_name_1')
You should see the following result:
Â
{ "nIndexesWas" : 2, "ok" : 1 }
- Let's recreate the index:
> db.mockdata.createIndex({'city':1}, {name: 'city_index'}) { ...