- Reference >
- MongoDB Package Components >
mongorestore
mongorestore¶
Synopsis¶
The mongorestore program writes data from a binary database
dump created by mongodump to a MongoDB
instance. mongorestore can create a new database or add
data to an existing database.
mongorestore can write data to either mongod or mongos
instances, in addition to writing directly to MongoDB data files
without an active mongod.
If you restore to an existing database, mongorestore will
only insert into the existing database, and does not perform updates
of any kind. If existing documents have the same value _id field
in the target database and collection,
mongorestore will not overwrite those documents.
Remember the following properties of mongorestore behavior:
mongorestorerecreates indexes recorded bymongodump.all operations are inserts, not updates.
mongorestoredoes not wait for a response from amongodto ensure that the MongoDB process has received or recorded the operation.The
mongodwill record any errors to its log that occur during a restore operation, butmongorestorewill not receive errors.
Options¶
-
mongorestore¶
-
--help¶ Returns a basic help and usage text.
-
--verbose,-v¶ Increases the amount of internal reporting returned on the command line. Increase the verbosity with the
-vform by including the option multiple times (e.g.-vvvvv).
-
--version¶ Returns the version of the
mongorestoretool.
-
--host<hostname><:port>¶ Specifies a resolvable hostname for the
mongodto which you want to restore the database. By defaultmongorestorewill attempt to connect to a MongoDB process running on the localhost port number27017. For an example of--host, see Restore a Database with mongorestore.Optionally, specify a port number to connect a MongoDB instance running on a port other than
27017.To connect to a replica set, you can specify the replica set seed name, and a seed list of set members, in the following format:
-
--port<port>¶ Specifies the port number, if the MongoDB instance is not running on the standard port (i.e.
27017). You may also specify a port number using the--hostcommand. For an example of--port, see Restore a Database with mongorestore.
-
--ipv6¶ Enables IPv6 support that allows
mongorestoreto connect to the MongoDB instance using an IPv6 network. All MongoDB programs and processes, includingmongorestore, disable IPv6 support by default.
-
--ssl¶ New in version 2.4: MongoDB added support for SSL connections to
mongodinstances in mongorestore.Note
SSL support in mongorestore is not compiled into the default distribution of MongoDB. See Connect to MongoDB with SSL for more information on SSL and MongoDB.
Additionally, mongorestore does not support connections to
mongodinstances that require client certificate validation.Allows
mongorestoreto connect tomongodinstance over an SSL connection.
-
--username<username>,-u<username>¶ Specifies a username to authenticate to the MongoDB instance, if your database requires authentication. Use in conjunction with the
--passwordoption to supply a password. For an example of--username, see Restore a Database with mongorestore.
-
--password<password>,-p<password>¶ Specifies a password to authenticate to the MongoDB instance. Use in conjunction with the
--usernameoption to supply a username. For an example of--password, see Restore a Database with mongorestore.If you specify a
--usernameand do not pass an argument to--password,mongorestorewill prompt for a password interactively. If you do not specify a password on the command line,--passwordmust be the last argument specified.
-
--authenticationDatabase<dbname>¶ New in version 2.4.
Specifies the database that holds the user’s (e.g
--username) credentials.By default,
mongorestoreassumes that the database specified to the--dbargument holds the user’s credentials, unless you specify--authenticationDatabase.See
userSource, system.users Privilege Documents and User Privilege Roles in MongoDB for more information about delegated authentication in MongoDB.
-
--authenticationMechanism<name>¶ New in version 2.4.
Specifies the authentication mechanism. By default, the authentication mechanism is
MONGODB-CR, which is the MongoDB challenge/response authentication mechanism. In MongoDB Enterprise,mongorestorealso includes support forGSSAPIto handle Kerberos authentication.See Deploy MongoDB with Kerberos Authentication for more information about Kerberos authentication.
-
--dbpath<path>¶ Specifies the directory of the MongoDB data files. If used, the
--dbpathoption enablesmongorestoreto attach directly to local data files and insert the data without themongod. To run with--dbpath,mongorestoreneeds to lock access to the data directory: as a result, nomongodcan access the same path while the process runs. For an example of--dbpath, see Restore Without a Running mongod.
-
--directoryperdb¶ Use the
--directoryperdbin conjunction with the corresponding option tomongod, which allowsmongorestoreto import data into MongoDB instances that have every database’s files saved in discrete directories on the disk. This option is only relevant when specifying the--dbpathoption.
-
--journal¶ Allows
mongorestoreto write to the durability journal to ensure that the data files will remain valid during the write process. This option is only relevant when specifying the--dbpathoption. For an example of--journal, see Restore Without a Running mongod.
-
--db<db>,-d<db>¶ Use the
--dboption to specify a database formongorestoreto restore data into. If the database doesn’t exist,mongorestorewill create the specified database. If you do not specify a<db>,mongorestorecreates new databases that correspond to the databases where data originated and data may be overwritten. Use this option to restore data into a MongoDB instance that already has data.--dbdoes not control which BSON filesmongorestorerestores. You must use themongorestorepath option to limit that restored data.
-
--collection<collection>,-c<collection>¶ Use the
--collectionoption to specify a collection formongorestoreto restore. If you do not specify a<collection>,mongorestoreimports all collections created. Existing data may be overwritten. Use this option to restore data into a MongoDB instance that already has data, or to restore only some data in the specified imported data set.
-
--objcheck¶ Forces the
mongorestoreto validate all requests from clients upon receipt to ensure that clients never insert invalid documents into the database. For objects with a high degree of sub-document nesting,--objcheckcan have a small impact on performance. You can set--noobjcheckto disable object checking at run-time.Changed in version 2.4: MongoDB enables
--objcheckby default, to prevent any client from inserting malformed or invalid BSON into a MongoDB database.
-
--noobjcheck¶ New in version 2.4.
Disables the default document validation that MongoDB performs on all incoming BSON documents.
-
--filter'<JSON>'¶ Limits the documents that
mongorestoreimports to only those documents that match the JSON document specified as'<JSON>'. Be sure to include the document in single quotes to avoid interaction with your system’s shell environment. For an example of--filter, see Restore a Subset of data from a Binary Database Dump.
-
--drop¶ Before restoring the collections from the dumped backup, drops the collections from the target database.
--dropdoes not drop collections that are not in the backup.
-
--oplogReplay¶ Replays the oplog after restoring the dump to ensure that the current state of the database reflects the point-in-time backup captured with the “
mongodump --oplog” command. For an example of--oplogReplay, see Restore Point in Time Oplog Backup.
-
--keepIndexVersion¶ Prevents
mongorestorefrom upgrading the index to the latest version during the restoration process.
-
--w<number of replicas per write>¶ New in version 2.2.
Specifies the write concern for each write operation that
mongorestorewrites to the target database. By default,mongorestoredoes not wait for a response for write acknowledgment.
-
--noOptionsRestore¶ New in version 2.2.
Prevents
mongorestorefrom setting the collection options, such as those specified by thecollModdatabase command, on restored collections.
-
--noIndexRestore¶ New in version 2.2.
Prevents
mongorestorefrom restoring and building indexes as specified in the correspondingmongodumpoutput.
-
--oplogLimit<timestamp>¶ New in version 2.2.
Prevents
mongorestorefrom applying oplog entries newer than the<timestamp>. Specify<timestamp>values in the form of<time_t>:<ordinal>, where<time_t>is the seconds since the UNIX epoch, and<ordinal>represents a counter of operations in the oplog that occurred in the specified second.You must use
--oplogLimitin conjunction with the--oplogReplayoption.
-
<path>¶ The final argument of the
mongorestorecommand is a directory path. This argument specifies the location of the database dump from which to restore.
Usage¶
See Back Up and Restore with MongoDB Tools
for a larger overview of mongorestore
usage. Also see the mongodump document for an overview of the
mongodump, which provides the related inverse
functionality.
Consider the following example:
Here, mongorestore reads the database dump in the dump/
sub-directory of the current directory, and restores only the
documents in the collection named people from the database named
accounts. mongorestore restores data to the instance
running on the localhost interface on port 27017.
In the next example, mongorestore restores a backup of the
database instance located in dump to a database instance stored
in the /srv/mongodb on the local machine. This requires that there
are no active mongod instances attached to /srv/mongodb
data directory.
In the final example, mongorestore restores a database
dump located at /opt/backup/mongodump-2011-10-24, to a database
running on port 37017 on the host
mongodb1.example.net. The mongorestore command authenticates to
the MongoDB instance using the username user and the
password pass, as follows: