Mongodb has two very useful commands for this purpose: mongodump
and mongorestore
. Here's how to use them:
-
mongodump: It is a utility for creating a binary export of the contents of a database. Basically, using this command you can export MongoDb database.
Use the following command to dump your local database:
mongodump --db <your_database_name> --out <directory_path>
For example:
mongodump --db myDatabase --out /myData
This command will create a dump of your database named
myDatabase
and will put it in a directory at/myData/myDatabase/
. -
Now, you can transfer these dumped files to another system by any means (e.g. scp, rsync, your USB stick, etc).
-
mongorestore: On the other system, use
mongorestore
utility to import the data.mongorestore
is a tool for restoring a binary backup.Use it like so:
mongorestore --db <new_database_name> <directory_path>
For example:
mongorestore --db newDatabase /myData/myDatabase/
This command will create (or overwrite) a MongoDB database named
newDatabase
using the dumped data stored at/myData/myDatabase/
.
Note:
- You may need to start mongodb service or give the host details in the command. The command for starting MongoDB service depends on the OS and installation method. For most of the Ubuntu systems it’s
sudo service mongod start
. - If your database is secured, you might need to add your username and password to these commands with the
-u <username>
and-p <password>
options.
Make sure to replace <your_database_name>
, <new_database_name>
, and <directory_path>
with your actual database names and path.
Great to hear that
mongodump
worked for you! To use the data you've dumped from your cloud database on your local MongoDB instance, you will usemongorestore
. This tool is designed to restore a binary backup created bymongodump
.Here's a basic step-by-step guide on how to do it:
Ensure MongoDB is Running on Your Local Machine:
mongo --version
to check the installation and version orsystemctl status mongod
(on Linux) to check if the MongoDB service is active.Using
mongorestore
:mongorestore
utility restores data from the dump files created bymongodump
. You will point it to the directory where you stored your dump./path/to/your/dump/directory
with the path to the directory where you stored the backup frommongodump
.Example:
~/Desktop/myDatabaseBackup
, you would use:mongorestore ~/Desktop/myDatabaseBackup
Restoring Specific Database or Collection (Optional):
mongorestore
.yourLocalDatabaseName
with the name you want for the database on your local MongoDB instance.Handling Errors or Issues:
Verification:
mongo
shell or a GUI tool like MongoDB Compass) and checking the contents of the restored database.Remember,
mongorestore
will add the data to your local MongoDB instance without overwriting the entire database, but it will overwrite existing collections if they have the same name as those in the backup. If you want to avoid this, consider restoring to a new database or removing the existing collections first.