How to Use the Command 'mongorestore' (with Examples)
The mongorestore
utility is a powerful tool provided by MongoDB for importing a collection or database from a binary dump back into a MongoDB instance. It is an essential part of the MongoDB suite, primarily used to restore data that has been backed up using the mongodump
utility. This command facilitates the recovery of databases by restoring them to a specific MongoDB instance, ensuring data redundancy and safety. It is particularly useful in scenarios where data migration, backup restoration, or transferring databases from one server to another is necessary.
Use Case 1: Import a BSON Data Dump from a Directory to a MongoDB Database
Code:
mongorestore --db database_name path/to/directory
Motivation:
In situations where you have a complete backup of a MongoDB database stored as a BSON data dump and you need to restore it to a new or existing MongoDB database, this command is ideal. For example, after a server crash or when transferring data to a new environment, this command efficiently restores your data, allowing you to pick up right where you left off.
Explanation:
--db database_name
: This argument specifies the name of the database where the BSON data should be restored. It ensures that the data is correctly matched with the intended database.path/to/directory
: The path to the directory where the BSON data dump is located. This directory should contain the backup created bymongodump
.
Example Output:
After running this command, you might see outputs similar to:
2023-10-15T10:20:30.123+0000 restoring to existing collection database_name.collection_name without dropping
2023-10-15T10:20:31.123+0000 restored 100/1000 documents
Use Case 2: Import a BSON Data Dump from a Directory to a Given Database in a MongoDB Server Host with User Authentication
Code:
mongorestore --host database_host:port --db database_name --username username path/to/directory --password
Motivation:
When accessing a MongoDB instance hosted on a remote server that requires authentication, this command comes in handy. It is often used in production environments where you need to restore databases securely to a specific server, avoiding unauthorized access while doing so.
Explanation:
--host database_host:port
: Specifies the server’s host address and port where the MongoDB instance is located. This allows the tool to connect directly to the desired MongoDB server.--db database_name
: Designates the database into which the backup will be restored.--username username
: The username credential required to authenticate with the server. This ensures operations are performed only by authorized personnel.path/to/directory
: Directory path holding the binary BSON data.--password
: A prompt to enter the password securely, preventing password exposure in the command line.
Example Output:
The command interaction will ask for the password, further producing outputs like:
Enter password: ***
2023-10-15T10:25:00.789+0000 restoring to existing collection database_name.collection_name without dropping
2023-10-15T10:25:05.789+0000 restored 1000/1000 documents
Use Case 3: Import a Collection from a BSON File to a MongoDB Database
Code:
mongorestore --db database_name path/to/file
Motivation:
This use case is beneficial when you have a specific collection backup in BSON format and need to restore just that collection into a database. It’s particularly useful for partial backups or when you want to selectively restore components of a larger database.
Explanation:
--db database_name
: Specifies the target database for the collection restoration.path/to/file
: Indicates the path to the specific BSON file containing the collection dump.
Example Output:
You might observe results such as:
2023-10-15T10:30:10.456+0000 restoring to existing collection database_name.collection_name without dropping
2023-10-15T10:30:11.456+0000 restored 2000/2000 documents
Use Case 4: Import a Collection from a BSON File to a Given Database in a MongoDB Server Host with User Authentication
Code:
mongorestore --host database_host:port --db database_name --username username path/to/file --password
Motivation:
When you need to restore a specific collection from a BSON file to a remote MongoDB server requiring authentication, especially when precision and security are needed, this command is your go-to solution.
Explanation:
--host database_host:port
: The target remote server’s address and port.--db database_name
: Specifies the database where the collection should be restored.--username username
: Authentication is performed with this username.path/to/file
: The BSON file path containing the desired collection.--password
: Prompts for secure password entry.
Example Output:
After providing the password, the output could be:
Enter password: ***
2023-10-15T10:35:20.123+0000 restoring to existing collection database_name.collection_name without dropping
2023-10-15T10:35:21.123+0000 restored 500/500 documents
Conclusion:
The ‘mongorestore’ command is an essential utility when managing MongoDB databases, allowing backups to be efficiently restored. Whether you’re dealing with complete databases or individual collections, hosted locally or on remote servers, and with or without authentication, ‘mongorestore’ provides the flexibility and security necessary for managing your data backups. Understanding these use cases empowers database administrators to safeguard their data and maintain business continuity effectively.