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.
LOCAL to CLOUD
To dump your local
copilotly-test
MongoDB database and restore it to your cloud MongoDB instance hosted at the given URI, you'll follow a two-step process. First, you'll usemongodump
to export your local database, and then usemongorestore
to import that data into your cloud MongoDB.Step 1: Dump your local MongoDB database
You'll need to run
mongodump
to export thecopilotly-test
database from your local MongoDB instance. If your MongoDB server is running with default settings (i.e., onlocalhost
with the default port27017
), you may not need to specify a URI for the local dump command. Here's how you do it:Replace
/path/to/your/local/directory
with the actual path where you want to store the dump files.Step 2: Restore the dump to your cloud MongoDB
After dumping your local database, the next step is to use
mongorestore
to import the data into your cloud MongoDB. You'll need to use the--uri
flag to specify the connection string for your cloud MongoDB instance. Here's how you can do it based on the URI you've provided:mongorestore --uri="mongodb+srv://admin:[email protected]/testdb?retryWrites=true&w=majority" /path/to/your/local/directory/copilotly-test
Remember to replace
/path/to/your/local/directory/copilotly-test
with the actual path to the directory containing your local database dump. Also, ensure that yourmongorestore
command points to the exact folder that contains the dump of thecopilotly-test
database.Additional Notes
By following these steps, you should be able to successfully dump your local
copilotly-test
database and restore it to your cloud MongoDB instance.