190 views
asked in MongoDB by
Backup and Restore MongoDB in Windows using both the command line and MongoDB Compass

1 Answer

answered by

inchirags@gmail.com   Chirag's MongoDB DBA Tutorial         https://www.chirags.in

*****************************************************************************************

*Backup and Restore MongoDB in Windows using both the command line and MongoDB Compass*

*****************************************************************************************

YouTube Video:

MongoDB Database server:

Server IP: 192.168.224.133

Steps:

1. Install MongoDB Tools

First, ensure that you have the MongoDB Database Tools installed, which includes mongodump and mongorestore:

Download MongoDB Tools:

Go to the MongoDB Tools download page.

https://www.mongodb.com/try/download/database-tools

Choose your operating system (Windows), download the ZIP file, and unzip it to a folder of your choice.

Add MongoDB Tools to Path (optional, but recommended for easier access):

Open Environment Variables settings in Windows.

Add the path to the folder where MongoDB Tools are installed (usually something like C:\mongodb-database-tools-windows-x86_64-xxxx\bin) to your PATH variable.

2. Create a Database

On the home screen, click "Create Database".

In the dialog box:

Database Name: Enter a name, e.g., mydatabase.

Collection Name: Enter a collection name, e.g., users.

Click "Create Database".

3. Add Collections

In the left-hand panel, select your newly created database (mydatabase).

Click the "Add Collection" button.

Enter a collection name, e.g., orders, and click "Create".

4. Insert Data into Collections

Insert data into the users collection:

Select the users collection.

Click "Insert Document".

Add data in JSON format. For example:

{
  "name": "Chirag Mahto",
  "email": "inchirags@gmail.com",
  "age": 35
}

Click "Insert".

Repeat this step to add more documents.

Insert data into the orders collection:

5. Select the orders collection.

Click "Insert Document".

Add data in JSON format. For example:

{
  "order_id": "123",
  "user": "chirag",
  "items": [
    { "item_name": "Laptop", "quantity": 1 },
    { "item_name": "Keyboard", "quantity": 1 }
  ],
  "total": 35000.00
}

Click "Insert".

6. Taking a Backup with mongodump (CMD)

mongodump creates a binary backup of your MongoDB data. Follow these steps:

Open Command Prompt:

Press Win + R, type cmd, and press Enter.

Run mongodump:

To back up the entire MongoDB database, run:

mongodump --uri="mongodb://localhost:27017" --out="C:\path\to\backup"

Replace "mongodb://localhost:27017" with your MongoDB connection URI, and "C:\path\to\backup" with the desired output directory for the backup files.

To back up a specific database:

mongodump --uri="mongodb://localhost:27017" --db=mydatabase --out="C:\path\to\backup"

Replace mydatabase with the name of the database you want to back up.

To back up a specific collection:

mongodump --uri="mongodb://localhost:27017" --db=mydatabase --collection=users --out="C:\path\to\backup"

Replace users with the name of the collection to back up.

Verify Backup:

Go to the specified output folder (C:\path\to\backup), where you’ll find a folder with your database dump files.

7. Restoring a Backup with mongorestore (CMD)

mongorestore restores data from binary backup files created by mongodump.

Open Command Prompt:

Press Win + R, type cmd, and press Enter.

Run mongorestore:

To restore the entire backup:

mongorestore --uri="mongodb://localhost:27017" --dir="C:\path\to\backup"

To restore a specific database:

mongorestore --uri="mongodb://localhost:27017" --db=mydatabase "C:\path\to\backup\mydatabase"

Replace mydatabase with the name of the database you want to restore.

To restore a specific collection:

mongorestore --uri="mongodb://localhost:27017" --db=mydatabase --collection=users "C:\path\to\backup\mydatabase\users.bson"

Verify Restore:

You can verify the restoration by connecting to MongoDB and checking the data.

8. Taking a Backup with MongoDB Compass

Open MongoDB Compass.

Navigate to the Database:

Connect to your MongoDB instance.

In the sidebar, select the database you want to back up.

Export Data:

Click on the Collection you want to back up.

Select Export Collection from the toolbar.

Choose either JSON or CSV format for the backup, depending on your preference.

Save the Exported Data:

Choose a location to save the file and export the data.

9. Restoring a Backup with MongoDB Compass

Open MongoDB Compass.

Navigate to the Database:

Connect to your MongoDB instance.

Import Data:

Select the database and collection where you want to restore the data.

Click Import Data from the toolbar and select the JSON or CSV file you exported.

Verify the Imported Data:

Check the collection to confirm the data was imported successfully.

For any doubts and query, please write on YouTube video comments section.

Note : Flow the Process shown in video.

Please, Subscribe and like for more videos:

https://youtube.com/@chiragstutorial

Don't forget to, Follow, Like,  Share &, Comment

Thanks & Regards,

Chitt Ranjan Mahto "Chirag"

_________________________________________________________________________________________

Note: All scripts used in this demo will be available in our website.

Link will be available in description.

#mongodb 

#mongodbtutorial 

#mongo

#mongosh

#mongodbtutorialforbeginners 

#chiragstutorial 

#chiragsdatabasetutorial 

#chirags 

#chiragsmahto

Install MongoDB 8,Authentication,MongoDB Tutorial,MongoDB Chirags Tutorial,MongoDB DBA Tutorial,Chirags Tutorial,Chirags DBA Tutorial,Chirag Mahto,Chitt Ranjan Mahto,Chirag DBA Tutorial,Database Tutorial,NoSQL Database Tutorial,MongoDB NoSQL Database,MongoDB Install,MongoDB Configuration,MongoDB Access,MongoDB Compas,Mongosh,Rockmongo,Mongo,MongoDB Authorization,MongoDB Authentication,MongoDB Authorisation

Most popular tags

laravel postgresql laravel-10 replication ha postgresql mongodb laravel-11 mongodb database mongodb tutorial ubuntu 24.04 lts streaming-replication mysql database laravel postgresql backup laravel login register logout database mysql php laravel 11 - login with otp valid for 10 minutes. user and admin registration user and admin login multiauth technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 gaurds zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux configure replica laravel 11 socialite login with google account google login kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase postgresql 16 to postgresql 17 postgresql migration letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5
...