Database Reference
In-Depth Information
Repairing a Collection's Indexes
If the validation process shows that the indexes are damaged, use the
reIndex()
function to reindex the affected
collection. In the example that follows, you use the
reIndex()
function to reindex the blog's
posts
collection to which
you added the
author
index previously:
$mongo
>use blog
> db.posts.reIndex()
{
"nIndexesWas" : 2,
"msg" : "indexes dropped for collection",
"nIndexes" : 2,
"indexes" : [
{
"key" : {
"_id" : 1
},
"ns" : "blog.posts",
"name" : "_id_"
},
{
"key" : {
"Author" : 1
},
"ns" : "blog.posts",
"name" : "Author_1"
}
],
"ok" : 1
}
The MongoDB server will drop all the current indexes on the collection and rebuild them; however, if you use the
database
repair
option, it will also run the
reIndex()
function on all the collections in the database.
Repairing a Collection's Datafiles
The best—and most dangerous—way to repair all of the datafiles in a database is to use either the server's
--repair
option or the db.
repairDatabase()
command in the shell. The latter repairs all the collection files in an individual
database, and then reindexes all of the defined indexes. However,
repairDatabase()
is not a suitable function to
run on a live server, because it will block any requests to the data while the datafiles are being rebuilt. This results in
blocking all reads and writes while the database repair completes. The following snippet shows the syntax for using
the
repairDatabase()
function:
$mongo
>use blog
>db.repairDatabase()
{ "ok" : 1 }