- Indexes >
- Manage Indexes
Manage Indexes¶
The following procedures provides some common procedures for managing existing indexes. For instructions on creating indexes, refer to the specific index type pages.
View Existing Indexes¶
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 list all indexes on all collections in a database, you can use the following operation in the mongo shell:
db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].getIndexes();
print("Indexes for " + collection + ":");
printjson(indexes);
});
MongoDB 3.0 deprecates direct access to the system.indexes collection.
For MongoDB 3.0 deployments using the WiredTiger storage engine, if you run db.getCollectionNames() and db.collection.getIndexes() from a version of the mongo shell before 3.0 or a version of the driver prior to 3.0 compatible version, db.getCollectionNames() and db.collection.getIndexes() will return no data, even if there are existing collections and indexes. For more information, see WiredTiger and Driver Version Compatibility.
Remove Indexes¶
To remove an index from a collection, you can use the db.collection.dropIndex() method. To rebuild indexes, see Rebuild Indexes instead.
Remove a Specific Index¶
To remove an index, use the db.collection.dropIndex() method.
For example, the following operation removes an ascending index on the tax-id field in the accounts collection:
db.accounts.dropIndex( { "tax-id": 1 } )
The operation returns a document with the status of the operation:
{ "nIndexesWas" : 3, "ok" : 1 }
Where the value of nIndexesWas reflects the number of indexes before removing this index.
For text indexes, pass the index name to the db.collection.dropIndex() method. See 使用索引名称来删除 文本 索引 for details.
Remove All Indexes¶
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.
Modify an Index¶
To modify an existing index, you need to drop and recreate the index with the exception of m TTL 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.
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 for a collection, including the _id index, and then rebuilds all indexes.
注解
For replica sets, db.collection.reIndex() will not propagate from the primary to secondaries. db.collection.reIndex() will only affect a single mongod instance.
重要
db.collection.reIndex() will rebuild indexes in the background if the index was originally specified with this option. However, db.collection.reIndex() will rebuild the _id index in the foreground, which takes the database’s write lock.
db.accounts.reIndex()
This shell helper provides a wrapper around the reIndex database command. Your client library may have a different or additional interface for this operation.
To build or rebuild indexes for a replica set, see 在复制集上创建索引.