Using mongorestore for Restoring MongoDB Backups

It is crucial to have an effective and reliable method of data restoration when backing up data after the restore and backup process. Think about the different aspects:

  • A properly-configured restoration technique that is properly configured allows users to successfully restore data back to its previous state.
  • An ineffective restoration technique renders the entire backup process unproductive because it prevents users from accessing and then restoring the backup data.

Mongorestore is the mongorestore command is the corresponding command to the command mongodump. You can restore dumps (backups) generated using the mongodump command in an MongoDB instance by using mongorestore.

In this post, you’ll be taught how to use Mongorestore to recover backups of your database efficiently.

(This post is part our MongoDB Guide. Make use of the menu on the right to navigate. )

What is Mongorestore?

mongorestore is a basic tool that can be used for restoring backups. It is able to load information from any of the following sources:

  • A dump file for databases generated by the mongodump command.
  • The input that is standard to the mongod or mongos instances

Since MongoDB 4.4 and later, the mongorestore tool isn’t included in the default MongoDB Server installation program. Instead, it’s available in a separate package inside the MongoDB Database Tools package. This permits the application to be able to utilize a distinct date of release starting at 100.0.0.

The utility for mongorestore provides support to MongoDB version 4.4, 4.2, 4.0 and 3.6. It could also work with older versions of MongoDB however compatibility isn’t guaranteed.

Furthermore, mongorestore supports a multitude of operating systems and platforms including x86 and s390x and you can view the complete list of compatibility options within mongorestore’s document that is officially published.

Mongorestore behavior

Here’s a list of important things to be aware of the expected behavior for the Mongorestore utility.

  • The mongorestore utility allows users to restore data into an existing database or to create an entirely new database. When restoring data to an existing database mongorestore does not use any insert functions and doesn’t make any updates. Due to this, any documents that already exist with a similar value for the field _id of the document that are in the backup won’t be altered by the process of restoration.

This can result in an error with a duplicate key in this restoration procedure, which is illustrated in this article:

  • Since mongodump is not able to backup indexes, mongorestore will create indexes that were recorded by mongodump.
  • The most effective method of recovering and backing up the database is to utilize the appropriate versions of both Mongodump as well as mongorestore. If a backup was created with a specific edition of the utility mongodump it is recommended to use the equivalent edition of the utility mongorestore in order to complete the restore process.
  • Mongorestore cannot be able to restore “system.profile” collection data.
  • The mongorestore application is compatible with FIPS ( Federal Information Processing Standard) connections for restoration operations.
  • When you restore data to an MongoDB instance that is controlled by access You must know the following situations:
    • If the restored data set does not contain”system.profile” in it “system.profile” collection and you use the mongorestore command with the option -oplogReply option The “restore” role will provide the necessary permissions for carrying restore operations.
    • If you are restoring backups that comprise those that include the “system.profile” collection, even although mongorestore will not restore the collection in question and will instead attempt to create a new “system.profile” collection. In this case you’ll need to utilize roles such as the “dbAdmin” and “dbAdminAnyDatabase” roles to grant the required permissions.
    • To run the mongorestore script using the -oplogReply option the user has to create a new role that is user-defined with anyAction permissions in anyResource.
  • Mongorestore isn’t a suitable utility as the backup process for backing up and restore sharded clusters using ongoing sharded transactions. This change was made by MongoDB 4.2. In the case of distributed clusters most recommended solution is MongoDB Cloud Manager or Ops Manager because they are able to keep the atomicity of transactions across clusters.
  • The mongorestore command must be executed through the command shell on the system as it is a distinct database application.

Using MongoDB mongorestore

This section you’ll discover the fundamental use of the mongorestore application as a stand-alone MongoDB instance.

Basic mongorestore syntax



The most basic method of restoring an existing database is by using the mongorestore command, which specifies your backup location (dump directory) without options. This method is appropriate for databases that are located on localhost ( that use the port number 27017. The restore procedure will create new databases and collections when required and also record the progress.Copy

mongorestore ./dumpmongorestore./dump

The result:

In the previous example you can see the steps it is possible to restore successfully your “vehicles” database with all its documents and collections. It will create a brand new database named vehicles within the MongoDB instance, which contains all documents and collections of the backup. You can test your restoration by accessing this MongoDB server.Copy

use vehicles show collections db.vehicleinformation.find()

The result:

Restoring data in the remote MongoDB instance

To restore data from the remote MongoDB instance, you must to create the connection. The connection to a database could be established using either of these methods:

  • The URI connection string
  • The host option
  • The port and host option

Connecting via this URI option:Copy

mongorestore with additional options --uri="mongodb http:/mongodb:/:" [restore directory/file]

Connecting via host:Copy

mongorestore with additional options host=":" [restore directory/file]

Connecting via ports and hosts:Copy

mongorestore with additional options host="" --port= [restore directory/file]

The below example demonstrates how to restore backups of your remote MongoDB instance. The verbose option provides users with an in-depth description of the process for restoration.Copy

mongorestore --verbose --host="" --port=27017 ./dump/


Restoring an encrypted MongoDB instance

If you want to connect with an access controlled MongoDB instance will need to supply:

  • Username
  • Password
  • Authentication database options

Furthermore, mongorestore allows key-based authentication. It is crucial to make sure that the user who has been authenticated has the appropriate rights and permissions to carry out this restoration procedure.

Authentication syntax:Copy

mongorestore [additional options] --authenticationDatabase=-u=-p= [restore directory/file]

The restoration command below shows the steps to make a connection to the remote MongoDB server by using your username as well as password used for authentication.Copy

mongorestore --host= --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" ./dump/


Selecting Collections and Databases

By using the -nsInclude parameter, users can indicate which collection or database is required to be restored. If you choose to include the option -nsInclude it is possible to use the namespace pattern (Ex: “vehicles. *”, “vehicles.vehicleInformation”) to define which database or collection should be included.

To define multiple namespaces, you can execute the -nsInclude command multiple times within a single command. The -nsInclude command can also support wildcards that can be included in the specified namespace.

The options -db or -collection are no longer supported and result in the error below.

To remove a database or collection using the -nsExclude command.

When selecting a database or collection:Copy

mongorestore with additional options --nsInclude= ($.$) [restore directory/file]

Excluding a Database/CollectionCopy

mongorestore with additional options --nsExclude= ($.$) [restore directory/file]

In the next instance, you’ll learn how to restore the entire “persons” database. It is possible to include the entire database by using”persons,” or the “persons” namespace with the an asterisk in a wild card pattern. This will allow you to restore all information in the database.Copy

mongorestore --nsInclude=persons. * --host= --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" ./dump/


Restore information to an Archive File

The mongorestore utility can restore using an archive file. The -archive option is used to choose the archive file along with the -nsInclude or -nsExclude options are available together with an archive choice.Copy

mongorestore (additional options) archive=

The following example shows the process of defining an archive file to restore data. The -nsInclude feature is used to define the collection to be transferred to the database using an archived file.Copy

mongorestore -v --nsInclude=vehicles.vehicleinformation --host= --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --archive=db.archive

The result:

Restoring information from an Compressed File

The Mongodump utility makes use of the option -gzip to compress particular JSON as well as BSON files. The backups compressed can be used to restore databases. The compressed backup file can be used to filter the database by using the -nsInclude or -nsExclude options.Copy

mongorestore --gzip [additional choices] [restore directory/file[restore directory/file

You can restore the compressed MongoDB backup by following the commands. The backup that is compressed is stored within the “backupzip” directory.Copy

mongorestore --gzip -v --nsInclude=vehicles.vehicleinformation --host= --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" ./backupzip/


The same procedure could be applied to archive files that are compressed. This example demonstrates how to retrieve information from an archive file that has been compressed.Copy

mongorestore --gzip -v --nsInclude=vehicles.vehicleinformation --host= --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --archive=db.archive

The result:

Restoring information from Standard Input

The mongorestore command lets users to access data from standard inputs and then use the data for restoration. It is possible to read the data using the -archive option with no filename.Copy

mongodump [additional choices] --archive | mongorestore [additional choices--archive

This example shows how to create a backup of an secure MongoDB database by using mongodump and then pass it as an input standard to the mongorestore command so that it can be restored on an unsecure distant MongoDB instance.Copy

mongodump --host= --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --db=vehicles --archive | mongorestore --host= --port=27018 --archive

The result:

You can confirm your restoration by comparing the databases on your remote servers. This can be accomplished through the execution of an JavaScript code with the”-eval” tag. Utilizing this command, the “listDatabases” admin command, you’ll be able to display all databases that are part of your distant MongoDB instance.Copy

mongo --host= --port=27018 --quiet --eval 'printjson(db.adminCommand(  ))'

The result:

mongorestore and mongodump

This tutorial provides an in-depth understanding of the MongoDB mongorestore tool and how it can be utilized to restore backups created using mongodump. The Mongorestore utility provides a quick and efficient method to restore backups of databases.

Combining mongodump and mongorestore can provide small-scale administrators of databases with a complete backup plan.

Leave a Reply

Your email address will not be published. Required fields are marked *