WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MongoDB · Basic · question 20 of 100

How do you perform a simple backup and restore operation in MongoDB?

📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

Performing backups and restores in MongoDB is a crucial task when it comes to data management. MongoDB provides several tools to perform backups and restores that can be customized to suit specific needs. In this answer, I’ll cover a simple backup and restore operation using the ‘mongodump‘ and ‘mongorestore‘ utilities.

## Backup

The ‘mongodump‘ utility is used to create backups of MongoDB databases. To create a backup of a database, execute the ‘mongodump‘ command followed by the name of the database you want to backup:

mongodump --db <database_name>

For example, to create a backup of a database named ‘mydatabase‘, the command would be:

mongodump --db mydatabase

This command creates a backup of the ‘mydatabase‘ database in a directory named ‘dump‘ in the current working directory.

You can also backup a single collection by specifying the ‘–collection‘ parameter:

mongodump --db <database_name> --collection <collection_name>

For example, to create a backup of a collection named ‘mycollection‘ within the ‘mydatabase‘ database, the command would be:

mongodump --db mydatabase --collection mycollection

This command creates a backup of the ‘mycollection‘ collection in a directory named ‘dump‘ in the current working directory.

You can also backup to a specific directory by specifying the ‘–out‘ parameter:

mongodump --db <database_name> --out <backup_directory>

For example, to create a backup of the ‘mydatabase‘ database in a directory named ‘mybackup‘, the command would be:

mongodump --db mydatabase --out mybackup

This command creates a backup of the ‘mydatabase‘ database in a directory named ‘mybackup‘ in the current working directory.

## Restore

The ‘mongorestore‘ utility is used to restore MongoDB database backups that were created using the ‘mongodump‘ command. To restore a backup, execute the ‘mongorestore‘ command followed by the path to the backup directory:

mongorestore <backup_directory>

For example, to restore a backup from a directory named ‘mybackup‘, the command would be:

mongorestore mybackup

This command restores the ‘mydatabase‘ database from the backup in the ‘mybackup‘ directory.

You can also restore a single collection from a backup by specifying the ‘–collection‘ parameter:

mongorestore --db <database_name> --collection <collection_name> <backup_directory>/<collection_file>

For example, to restore a collection named ‘mycollection‘ within the ‘mydatabase‘ database from a backup in a directory named ‘mybackup‘, the command would be:

mongorestore --db mydatabase --collection mycollection mybackup/mycollection.bson

This command restores the ‘mycollection‘ collection from the ‘mybackup/mycollection.bson‘ backup file.

### Conclusion

In summary, performing backups and restores in MongoDB is essential for data management. The combination of ‘mongodump‘ and ‘mongorestore‘ is a straightforward way to create and restore backups. With this simple guide, one can execute a successful backup and restore operation.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MongoDB interview — then scores it.
📞 Practice MongoDB — free 15 min
📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

All 100 MongoDB questions · All topics