Database Reference
In-Depth Information
To remove a single index from a collection, you use syntax that mirrors the syntax used to create the index with
ensureIndex() :
>db.posts.dropIndex({"author.name":1, "author.email":1});
Reindexing a Collection
If you suspect that the indexes in a collection are damaged—for example, if you're getting inconsistent results to your
queries—then you can force a reindexing of the affected collection.
This will force MongoDB to drop and re-create all the indexes on the specified collection (see Chapter 9 for more
information on how to detect and solve problems with your indexes), as in the following example:
> db.posts.reIndex()
{
"nIndexesWas" : 2,
"msg" : "indexes dropped for collection",
"nIndexes" : 2,
"indexes" : [
{
"key" : {
"_id" : 1
},
"ns" : "blog.posts",
"name" : "_id_"
},
{
"key" : {
"Tags" : 1
},
"ns" : "blog.posts",
"name" : "Tags_1"
}
],
"ok" : 1
}
The output lists all the indexes the command has rebuilt, including the keys. Also, the nIndexWas: field
shows how many indexes existed before running the command, while the nIndex: field gives the total number of
indexes after the command has completed. If the two values are not the same, the implication is that there was
a problem re-creating some of the indexes in the collection.
How MongoDB Selects Which Indexes It Will Use
When a database system needs to run a query, it has to assemble a query plan , which is a list of steps it must run
to perform the query. Each query may have multiple query plans that could produce the same result equally well.
However, each plan may have elements in it that are more expensive to execute than others. For example, a scan of all
the records in a collection is an expensive operation, and any plan that incorporates such an approach could be slow.
These plans can also include alternative lists of indexes to use for query and sort operations.
Road directions serve as a good illustration of this concept. If you want to get to the diagonally opposite side of
a block from one corner, then “take a left, then take a right” and “take a right, then take a left” are equally valid plans
 
Search WWH ::




Custom Search