“MongoDB database provides an additional feature of restoring the deleted data. Like the restore feature in Windows, once our file is deleted, it can be restored from the recycle bin using the restore option. Similarly, MongoDB restores feature is used to fetch the file back to the place where it was before deletion. Mongorestore works in collaboration with mongodump. We have used some examples to explain their work. This article will provide you with knowledge regarding restoring the backup files in MongoDB.
For the backup and restore purpose in MongoDB, we need to use some additional features to the database; these are the MongoDB database tools.
Note: Once you have installed the tools, you need to add the path in the environmental variable to effectively use all the features in the database tools in the terminal. This path is the location of the Bin folder present inside the tools folder of MongoDB.”
Difference Between Mongodump and Mongorestore
There is an ambiguity between the working of mongodump and mongorestore.
- Mongodump is creating a clone or copy of the folder that already exists. It is done to save the file from unwanted data loss. By creating a backup, your data is saved. For instance, if the original file gets corrupted for some reason, your data will be saved in the copied one.
- Contrary to mongodump, mongorestore is a feature to restore the backup data. This opposite of the mongodump feature is used to restore the database. It restores the data by using a utility of BSON data dumps of mongodump. By using mongorestore, the default folder in mongodump’s bin or the dump folder is used.
To implement the mongorestore feature, we first go to the mongo shell to see all the databases present. Among all of them, demo and second are two databases that the user creates. Whereas the other three are the default databases.
After looking at the databases, we will now quit from the mongo shell by using Ctrl + C.
The user terminal is used to carry out all the mongorestore commands. Like mongodump, as we can create a backup of databases and collections separately, we can also restore the deleted data to the MongoDB folder.
First, we will use a simple keyword mongorestore. When there is no path mentioned, this will restore the data in the bin folder of mongodump. This will restore the data of all the databases and collections inside them that are either deleted or not deleted.
The above snap displays the details of each database and collection. All the data is first read, and the path specifies the location from which mongorestore will restore data.
Another way of restoring all data is simply using the dump folder as a path in the mongorestore command. This will work so that all the restored items will be again restored in a separate file, including all MongoDB data.
Restore Dump Collection
To restore the backup collection, we first need to create a backup of a specific collection. So we have selected a collection that is data that is present in the demo database. Now we will use a mongo dump command to create a backup of the collection. This command includes the information of the database, collection name, which you want to make a backup file, and most importantly, the path, the location where you want to create the folder. “-out” is used to save the file.
The response of this command will show that dumping is done without any interruption. A folder with the collection name “data” is created with the “bson” extension in the same path you have declared in the command. You can see that by going to the file manager.
Now we will apply the mongo restore command directly to the backup folder. Provide the exact path in the command to avoid any exception from occurring.
This will only restore the database and the collection name, not all the documents inside the collection, because we have not provided the destination path to store the data at that point. So the command will find this source file as the destination point too. All the documents get duplicated, and restoration of these documents will be failed. The solution to this problem, or you can say that the best results in the restoring process are obtained when you delete the original or the backup file. And then apply the restore command.
Restore Deleted Collection
So first, we will go to the mongo shell and then switch to the demo database and show all the collections.
Then drop the data collection by using a drop command. At the same time, when we check all the collections, you will see that “data” is missing.
[cc lang="sql" width="100%" height="100%" ESCAPED="true" theme="blackboard" nowrap="0"]>> SHOW collections
Now simply use the restore command with the path name at which you want to restore data. We have used the main database path, where the original collection file is present.
Observing the last line of the resultant, you can see that all 4 documents are restored, and there are 0 failures. Again go back to mongo to assure the restoration of data collection.
Use the show collection command.
You will see that the “data” collection is restored.
Conclusion
The main purpose of writing this article is to demonstrate the difference and, at the same time, the working of mongodump and mongorestore tools that play an important role in creating a copy of data and restoring the deleted file. You may come across such a situation where you unintentionally delete any database or collection from MongoDB, either through the command or manually from the folder. To overcome this inconvenience, use mongorestore.