Exporting Data from MongoDB (Backup)

In this article, we will learn how to export data (take a backup) from a remote MongoDB database by example. In this example, we have a remote database hosted on MongoDB atlas and we want to take a copy from this database on our local machine.

First Step Get Your IP

In this step run the following command to get your local machine IP

dig +short myip.opendns.com @resolver1.opendns.com

The out put will be your local machine IP

Second Step add this IP in MongoDB atlas

In network access add this IP like the below image

enter image description here

Third step change your DNS

Change the DNS IP to be 8.8.8.8 in this file /etc/resolv.conf like the below image
enter image description here

Last step mongodump

By running the following command a new folder created inside your current directory with the same database name and inside this directory you will find your collections with .bson extention

mongodump --uri 'PUT_DB_HERE_URI' --out $(pwd)

Finally to restore these data in your local machine run the following command

mongorestore --host 127.0.0.1 --port 27017 -d database_name ./exported_folder_which_contains_.bson files/

Subscribe to our Newsletter

Subscribe to tech-hour website to get all updates, new articles, new courses and projects. No spam ever. Unsubscribe at any time.