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.
If your MongoDB database (
myDatabase) is hosted in the cloud (for instance, on MongoDB Atlas, AWS, Azure, etc.), you can still usemongodumpto export data from it, but you'll need to specify additional connection parameters.Here is a generalized command structure to perform
mongodumpfrom a cloud-hosted MongoDB:mongodump --uri="mongodb+srv://username:password@yourMongoDBHost/yourDatabaseName" --out /path/to/your/local/directoryReplace the following placeholders with your actual details:
username: Your MongoDB username.password: Your MongoDB password.yourMongoDBHost: The address of your MongoDB server or cluster. If you're using MongoDB Atlas, this will be the connection string provided by Atlas.yourDatabaseName: The name of your database./path/to/your/local/directory: The local directory where you want to store the backup.For example, if your MongoDB is hosted on Atlas and your database name is
myDatabase, your command might look something like this:In this example:
myUserwith your MongoDB username.myPasswordwith your MongoDB password.mycluster.mongodb.netwith your Atlas cluster's address.myDatabaseBackupon your desktop.Some important notes:
myUserhas the necessary permissions to perform a dump onmyDatabase.mongodumptool must be installed on your local machine, and you should have the necessary permissions to write to the specified local directory.--sslto the command. MongoDB Atlas usually requires this.Remember to handle sensitive information like your username and password securely and avoid exposing them in shared scripts or command histories.