LinuxCommandLibrary

mongorestore

Restore MongoDB database from backup

TLDR

Import a BSON data dump from a directory to a MongoDB database

$ mongorestore --db [database_name] [path/to/directory]
copy

Import a BSON data dump from a directory to a given database in a MongoDB server host, running at a given port, with user authentication (user will be prompted for password)
$ mongorestore --host [database_host:port] --db [database_name] --username [username] [path/to/directory] --password
copy

Import a collection from a BSON file to a MongoDB database
$ mongorestore --db [database_name] [path/to/file]
copy

Import a collection from a BSON file to a given database in a MongoDB server host, running at a given port, with user authentication (user will be prompted for password)
$ mongorestore --host [database_host:port] --db [database_name] --username [username] [path/to/file] --password
copy

SYNOPSIS

mongorestore [options] [connection-string]

PARAMETERS

--uri
    The MongoDB connection string. Specifies the host, port, authentication, and other connection options.

--db
    The database to restore to.

--collection
    The collection to restore.

--drop
    Drops each collection from the target database before restoring.

--gzip
    Enables gzip compression for faster data transfer.

--archive=
    Restore data from a specified archive file. It can be used instead of specifying a directory.

--dir
    The directory containing the dumped BSON files.

--oplogReplay
    Replays the oplog entries available in the oplog collection from the dump.

--numInsertionWorkers
    Specifies the number of insertion worker threads to use. Defaults to 1.

--username
    Specify a username with which to authenticate to MongoDB if your database uses authentication.

--password
    Specify a password with which to authenticate to MongoDB if your database uses authentication.

--authenticationDatabase
    The database in which the user's credentials are stored.

--ssl
    Enable SSL connection to MongoDB.

DESCRIPTION

mongorestore is a command-line utility used to restore MongoDB database backups created by mongodump. It reads data from a BSON dump or archive and inserts it into a MongoDB database. This allows for restoring entire databases, specific collections, or only specific parts of collections based on your need.
It can connect to a MongoDB instance using various authentication mechanisms and connection string options. It's designed to be reliable and handle large datasets efficiently, making it a crucial tool for database administrators and developers working with MongoDB.
It provides granular control over the restore process. The utility supports compression for faster data transfer and storage, and it includes options to control indexing behavior, error handling, and parallel processing for optimal performance.

CAVEATS

Requires a compatible version of mongod running to connect to. Using `--drop` can cause permanent data loss. Ensure the backup directory or archive is accessible and contains valid MongoDB data.

PERFORMANCE TUNING

Using `--numInsertionWorkers` can significantly improve restore performance, especially for large datasets. Experiment with different values to find the optimal setting for your hardware.

AUTHENTICATION

Always use secure authentication when restoring to a production environment. Verify that the user specified with `--username` and `--password` has the necessary privileges to perform the restore operation.

HISTORY

mongorestore was developed alongside MongoDB as a core utility for managing database backups. It's evolved over time to support new MongoDB features like authentication mechanisms, compression, and enhanced performance.

SEE ALSO

mongodump(1), mongo(1), mongostat(1)

Copied to clipboard