Navigation
This version of the documentation is archived and no longer supported.

repairDatabase

repairDatabase

Warning

In general, if you have an intact copy of your data, such as would exist on a very recent backup or an intact member of a replica set, do not use repairDatabase or related options like db.repairDatabase() in the mongo shell or mongod --repair. Restore from an intact copy of your data.

Note

When using journaling, there is almost never any need to run repairDatabase. In the event of an unclean shutdown, the server will be able restore the data files to a pristine state automatically.

The repairDatabase command checks and repairs errors and inconsistencies with the data storage. The command is analogous to a fsck command for file systems.

If your mongod instance is not running with journaling the system experiences an unexpected system restart or crash, and you have no other intact replica set members with this data, you should run the repairDatabase command to ensure that there are no errors in the data storage.

As a side effect, the repairDatabase command will compact the database, as the compact command, and also reduces the total size of the data files on disk. The repairDatabase command will also recreate all indexes in the database.

Use the following syntax:

{ repairDatabase: 1 }

Be aware that this command can take a long time to run if your database is large. In addition, it requires a quantity of free disk space equal to the size of your database. If you lack sufficient free space on the same volume, you can mount a separate volume and use that for the repair. In this case, you must run the command line and use the --repairpath switch to specify the folder in which to store the temporary repair files.

Warning

This command obtains a global write lock and will block other operations until it has completed.

This command is accessible via a number of different avenues. You may:

  • Use the shell to run the above command, as above.

  • Use the db.repairDatabase() in the mongo shell.

  • Run mongod directly from your system’s shell. Make sure that mongod isn’t already running, and that you issue this command as a user that has access to MongoDB’s data files. Run as:

    $ mongod --repair
    

    To add a repair path:

    $ mongod --repair --repairpath /opt/vol2/data
    

    Note

    This command will fail if your database is not a master or primary. In most cases, you should recover a corrupt secondary using the data from an existing intact node. If you must repair a secondary or slave node, first restart the node as a standalone mongod by omitting the --replSet or --slave options, as necessary.