Index Management
Index management involves instructions for managing indexes and assessing index performance and use.
Remove Indexes – To remove an index from a collection use the dropIndex() method and the following procedure. If you simply need to rebuild indexes you can use the process described in the Rebuild Indexes document. To remove an index, use the db.collection.dropIndex() method, as in the following example
db.accounts.dropIndex( { “tax-id”: 1 } )
This will remove the index on the “tax-id” field in the accounts collection. The shell provides the following document after completing the operation:
{ “nIndexesWas” : 3, “ok” : 1 }
Where the value of nIndexesWas reflects the number of indexes before removing this index. You can also use the db.collection.dropIndexes() to remove all indexes, except for the _id index from a collection. These shell helpers provide wrappers around the dropIndexes database command. Your client library may have a different or additional interface for these operations.
Rebuild Indexes – If you need to rebuild indexes for a collection you can use the db.collection.reIndex() method to rebuild all indexes on a collection in a single operation. This operation drops all indexes, including the _id index, and then rebuilds all indexes. The operation takes the following form
db.accounts.reIndex()
MongoDB will return the following document when the operation completes:
{
“nIndexesWas” : 2,
“msg” : “indexes dropped for collection”,
“nIndexes” : 2,
“indexes” : [
{
“key” : {
“_id” : 1,
“tax-id” : 1
},
“ns” : “records.accounts”,
“name” : “_id_”
}
],
“ok” : 1
}
This shell helper provides a wrapper around the reIndex database command. Your client library may have a different or additional interface for this operation.
Manage In-Progress Index Creation – To see the status of the indexing processes, you can use the db.currentOp() method in the mongo shell. The value of the query field and the msg field will indicate if the operation is an index build. The msg field also indicates the percent of the build that is complete. To terminate an ongoing index build, use the db.killOp() method in the mongo shell. Changed in version 2.4: Before MongoDB 2.4, you could only terminate background index builds. After 2.4, you can terminate any index build, including foreground index builds.
Return a List of All Indexes – When performing maintenance you may want to check which indexes exist on a collection. Every index on a collection has a corresponding document in the system.indexes collection, and you can use standard queries (i.e. find()) to list the indexes, or in the mongo shell, the getIndexes() method to return a list of the indexes on a collection, as in the following examples.
List all Indexes on a Collection – To return a list of all indexes on a collection, use the db.collection.getIndexes() method or a similar method for your driver. For example, to view all indexes on the people collection
db.people.getIndexes()
List all Indexes for a Database – To return a list of all indexes on all collections in a database, use the following operation in the mongo shell
db.system.indexes.find()
Measure Index Use – Query performance is a good general indicator of index use; however, for more precise insight into index use, MongoDB provides a number of tools that allow you to study query operations and observe index use for your database.
Return Query Plan with explain() – Append the explain() method to any cursor (e.g. query) to return a document with statistics about the query process, including the index used, the number of documents scanned, and the time the query takes to process in milliseconds.
Control Index Use with hint() – Append the hint() to any cursor (e.g. query) with the index as the argument to force MongoDB to use a specific index to fulfill the query. Consider the following example
db.people.find( { name: “John Doe”, zipcode: { $gt: “63000” } } } ).hint( { zipcode: 1 } )
You can use hint() and explain() in conjunction with each other to compare the effectiveness of a specific index. Specify the $natural operator to the hint() method to prevent MongoDB from using any index:
db.people.find( { name: “John Doe”, zipcode: { $gt: “63000” } } } ).hint( { $natural: 1 } )
Instance Index Use Reporting – MongoDB provides a number of metrics of index use and operation that you may want to consider when analyzing index use for your database:
- In the output of serverStatus, check indexCounters, scanned and scanAndOrder
- In the output of collStats, check totalIndexSize and indexSizes
- In the output of dbStats, check dbStats.indexes and dbStats.indexSize
Apply for MongoDB Certification Now!!
https://www.vskills.in/certification/databases/mongodb-server-administrator