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 /myDataThis command will create a dump of your database named
myDatabaseand 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
mongorestoreutility to import the data.mongorestoreis 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
newDatabaseusing 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-testMongoDB database and restore it to your cloud MongoDB instance hosted at the given URI, you'll follow a two-step process. First, you'll usemongodumpto export your local database, and then usemongorestoreto import that data into your cloud MongoDB.Step 1: Dump your local MongoDB database
You'll need to run
mongodumpto export thecopilotly-testdatabase from your local MongoDB instance. If your MongoDB server is running with default settings (i.e., onlocalhostwith 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/directorywith 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
mongorestoreto import the data into your cloud MongoDB. You'll need to use the--uriflag 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:admin@cluster0.bhmosab.mongodb.net/testdb?retryWrites=true&w=majority" /path/to/your/local/directory/copilotly-testRemember to replace
/path/to/your/local/directory/copilotly-testwith the actual path to the directory containing your local database dump. Also, ensure that yourmongorestorecommand points to the exact folder that contains the dump of thecopilotly-testdatabase.Additional Notes
By following these steps, you should be able to successfully dump your local
copilotly-testdatabase and restore it to your cloud MongoDB instance.