Introduction
Migrating a MongoDB database between different hosts can be necessary for several reasons, such as moving your local database to a remote server, creating backup copies, or even pulling production data for local testing and development.
Thankfully, MongoDB offers an effortless way to handle this through mongodump and mongorestore, a powerful tool that simplifies the migration process.
Whether you're switching between MongoDB Atlas accounts or moving from a local to a cloud database, this guide will show you how to migrate your data seamlessly in just a few steps.
Step 1: Installing MongoDB Database Tools
MongoDB Database Tools are command-line utilities that help you manage and move data.
Install these tools on your platform to get started.
You can download the tool by clicking here
Step 2: Setting up Environment Variable
- Go to the path where the MongoDB Database Tools is installed and the copy the path until the
/binas shown.
- Then open the
Environment Variablesand within theUser variableupdate the path.
- Select "PATH", click "Edit" , click "New" => Add path which the mongodb data tools is
C:\Program Files\MongoDB\Tools\100\binand then click onOk.
Step 3: Dump Data to a Directory
mongodump --uri "SOURCE-CONNECTION-STRING-URI"
- 'SOURCE-CONNECTION-STRING-URI' is URL you are using to connect your app to MongoDB
-- either local something like
mongodb://127.0.0.1/someDatabaseor can be remote server likemongodb+srv://yourapp:<password>@cluster0.xyz123.mongodb.net/someDatabase
- The command will save a dump of your entire database into a folder and in the directory where the command was run.
Step 4: Restore Data from Directory
mongorestore --uri "DESTINATION-CONNECTION-STRING-URI"
- Run the above command in the same folder where the
dumpwas created and that's it.
Conclusion
In conclusion, this short tutorial showcased how easily data can be transferred between MongoDB instances using mongodump and mongorestore.
With these easy steps, you can quickly back up and restore your data with no hassle.
If you found this helpful, please like this post and follow for more useful tips!.




Top comments (0)