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.
If your MongoDB database (
myDatabase
) is hosted in the cloud (for instance, on MongoDB Atlas, AWS, Azure, etc.), you can still usemongodump
to export data from it, but you'll need to specify additional connection parameters.Here is a generalized command structure to perform
mongodump
from a cloud-hosted MongoDB:mongodump --uri="mongodb+srv://username:password@yourMongoDBHost/yourDatabaseName" --out /path/to/your/local/directory
Replace 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:
myUser
with your MongoDB username.myPassword
with your MongoDB password.mycluster.mongodb.net
with your Atlas cluster's address.myDatabaseBackup
on your desktop.Some important notes:
myUser
has the necessary permissions to perform a dump onmyDatabase
.mongodump
tool must be installed on your local machine, and you should have the necessary permissions to write to the specified local directory.--ssl
to 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.