- Reference >
- MongoDB Package Components >
mongorestore
mongorestore¶
macOS Sierra and Go 1.6 Incompatibility
Users running on macOS Sierra require the 3.2.10 or newer version of mongorestore.
Synopsis¶
The mongorestore program loads data from either a binary
database dump created by mongodump or the standard input
(starting in version 3.0.0) into a mongod or
mongos instance.
For an overview of mongorestore usage, see
Back Up and Restore with MongoDB Tools.
Syntax¶
Run mongorestore from the system command line, not the mongo shell.
For example, to restore from a dump directory to a local
mongod instance running on port 27017:
As mongorestore restores from the dump/ directory,
it creates the database and collections as needed and logs its progress:
You can also restore a specific collection or collections from the
dump/ directory. For example, the following operation restores a
single collection from corresponding data files in the dump/
directory:
If the dump/ directory does not contain the corresponding data file
for the specified namespace, no data will be restored. For example, the
following specifies a collection namespace that does not have a
corresponding data in the dump/ directory:
The mongorestore outputs the following messages:
For more examples, see Examples.
For more information on the options and arguments, see Options.
Behavior¶
Insert Only¶
mongorestore can create a new database or add data to an
existing database. However, mongorestore performs inserts
only and does not perform updates. That is, if restoring documents to
an existing database and collection and existing documents have the
same value _id field as the to-be-restored documents,
mongorestore will not overwrite those documents.
Rebuild Indexes¶
mongorestore recreates indexes recorded by
mongodump.
Note
Starting in MongoDB 2.6, creating indexes will error if an index key in an existing document exceeds the limit. See Enforce Index Key Length Limit for more information and solution.
If you have an existing data set that violates this limit but want
to resolve the index issue after restoring the data, you can disable
the default index key length validation on the target database by
setting the mongod instance’s
failIndexKeyTooLong parameter to false.
Version Compatibility¶
The data format used by mongodump from version 2.2 or
later is incompatible with earlier versions of mongod.
Do not use recent versions of mongodump to back up older
data stores.
Exclude system.profile Collection¶
mongorestore does not restore the system.profile collection data.
Required Access¶
To restore data to a MongoDB deployment that has access control enabled, the restore role provides
the necessary privileges to restore data from backups if the data does
not include system.profile
collection data and you run mongorestore without the
--oplogReplay option.
If the backup data includes system.profile collection data or you run with
--oplogReplay, you need
additional privileges:
system.profile |
If the backup data includes Both the built-in roles |
--oplogReplay |
To run with Grant only to users who must run |
Options¶
Changed in version 3.0.0: mongorestore removed the --filter, --dbpath, and the
--noobjcheck options.
-
mongorestore¶
-
--help¶ Returns information on the options and use of mongorestore.
-
--verbose,-v¶ Increases the amount of internal reporting returned on standard output or in log files. Increase the verbosity with the
-vform by including the option multiple times, (e.g.-vvvvv.)
-
--quiet¶ Runs mongorestore in a quiet mode that attempts to limit the amount of output.
This option suppresses:
- output from database commands
- replication activity
- connection accepted events
- connection closed events
-
--version¶ Returns the mongorestore release number.
-
--uri<connectionString>¶ New in version 3.4.6.
Specify a resolvable URI connection string to connect to the MongoDB deployment.
For more information on the components of the connection string, see the Connection String URI Format documentation.
Important
The following command-line options cannot be used in conjunction with
--urioption:--host--port--db--username--password(if the URI connection string also includes the password)--authenticationDatabase--authenticationMechanism
Instead, specify these options as part of your
--uriconnection string.
-
--host<hostname><:port>,-h<hostname><:port>¶ Default: localhost:27017
Specifies a resolvable hostname for the
mongodto which to connect. By default, the mongorestore attempts to connect to a MongoDB instance running on the localhost on port number27017.To connect to a replica set, specify the
replSetNameand a seed list of set members, as in the following:When specifying the replica set list format, mongorestore always connects to the primary.
You can also connect to any single member of the replica set by specifying the host and port of only that member:
Changed in version 3.0.0: If you use IPv6 and use the
<address>:<port>format, you must enclose the portion of an address and port combination in brackets (e.g.[<address>]).
-
--port<port>¶ Default: 27017
Specifies the TCP port on which the MongoDB instance listens for client connections.
-
--ssl¶ New in version 2.6.
Enables connection to a
mongodormongosthat has TLS/SSL support enabled.For more information about TLS/SSL and MongoDB, see Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients .
-
--sslCAFile<filename>¶ New in version 2.6.
Specifies the
.pemfile that contains the root certificate chain from the Certificate Authority. Specify the file name of the.pemfile using relative or absolute paths.Starting in version 3.4, if
--sslCAFileorssl.CAFileis not specified and you are not using x.509 authentication, the system-wide CA certificate store will be used when connecting to an TLS/SSL-enabled server.If using x.509 authentication,
--sslCAFileorssl.CAFilemust be specified.Warning
Version 3.2 and earlier: For TLS/SSL connections (
--ssl) tomongodandmongos, if the mongorestore runs without the--sslCAFile, mongorestore will not attempt to validate the server certificates. This creates a vulnerability to expiredmongodandmongoscertificates as well as to foreign processes posing as validmongodormongosinstances. Ensure that you always specify the CA file to validate the server certificates in cases where intrusion is a possibility.For more information about TLS/SSL and MongoDB, see Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients .
-
--sslPEMKeyFile<filename>¶ New in version 2.6.
Specifies the
.pemfile that contains both the TLS/SSL certificate and key. Specify the file name of the.pemfile using relative or absolute paths.This option is required when using the
--ssloption to connect to amongodormongosthat hasCAFileenabled withoutallowConnectionsWithoutCertificates.For more information about TLS/SSL and MongoDB, see Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients .
-
--sslPEMKeyPassword<value>¶ New in version 2.6.
Specifies the password to de-crypt the certificate-key file (i.e.
--sslPEMKeyFile). Use the--sslPEMKeyPasswordoption only if the certificate-key file is encrypted. In all cases, the mongorestore will redact the password from all logging and reporting output.If the private key in the PEM file is encrypted and you do not specify the
--sslPEMKeyPasswordoption, the mongorestore will prompt for a passphrase. See TLS/SSL Certificate Passphrase.For more information about TLS/SSL and MongoDB, see Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients .
-
--sslCRLFile<filename>¶ New in version 2.6.
Specifies the
.pemfile that contains the Certificate Revocation List. Specify the file name of the.pemfile using relative or absolute paths.For more information about TLS/SSL and MongoDB, see Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients .
-
--sslAllowInvalidCertificates¶ New in version 2.6.
Bypasses the validation checks for server certificates and allows the use of invalid certificates. When using the
allowInvalidCertificatessetting, MongoDB logs as a warning the use of the invalid certificate.Starting in MongoDB 3.4.16, if you specify
--sslAllowInvalidCertificatesorssl.allowInvalidCertificates: truewhen using x.509 authentication, an invalid certificate is only sufficient to establish a TLS/SSL connection but is insufficient for authentication.Warning
For TLS/SSL connections to
mongodandmongos, avoid using--sslAllowInvalidCertificatesif possible and only use--sslAllowInvalidCertificateson systems where intrusion is not possible.If the
mongoshell (and other MongoDB Tools) runs with the--sslAllowInvalidCertificatesoption, themongoshell (and other MongoDB Tools) will not attempt to validate the server certificates. This creates a vulnerability to expiredmongodandmongoscertificates as well as to foreign processes posing as validmongodormongosinstances.For more information about TLS/SSL and MongoDB, see Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients .
-
--sslAllowInvalidHostnames¶ New in version 3.0.
Disables the validation of the hostnames in TLS/SSL certificates. Allows mongorestore to connect to MongoDB instances even if the hostname in their certificates do not match the specified hostname.
For more information about TLS/SSL and MongoDB, see Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients .
-
--sslFIPSMode¶ New in version 2.6.
Directs the mongorestore to use the FIPS mode of the installed OpenSSL library. Your system must have a FIPS compliant OpenSSL library to use the
--sslFIPSModeoption.Note
FIPS-compatible TLS/SSL is available only in MongoDB Enterprise. See Configure MongoDB for FIPS for more information.
-
--username<username>,-u<username>¶ Specifies a username with which to authenticate to a MongoDB database that uses authentication. Use in conjunction with the
--passwordand--authenticationDatabaseoptions.Note
You cannot specify both
--usernameand--uri.
-
--password<password>,-p<password>¶ Specifies a password with which to authenticate to a MongoDB database that uses authentication. Use in conjunction with the
--usernameand--authenticationDatabaseoptions.Changed in version 3.0.2: To prompt the user for the password, pass the
--usernameoption without--passwordor specify an empty string as the--passwordvalue, as in--password "".Note
You cannot specify both
--passwordand--uri.
-
--authenticationDatabase<dbname>¶ Specifies the authentication database where the specified
--usernamehas been created. See Authentication Database.Note
You cannot specify both
--authenticationDatabaseand--uri.
-
--authenticationMechanism<name>¶ Default: SCRAM-SHA-1
Changed in version 2.6: Added support for the
PLAINandMONGODB-X509authentication mechanisms.Changed in version 3.0: Added support for the
SCRAM-SHA-1authentication mechanism. Changed default mechanism toSCRAM-SHA-1.Specifies the authentication mechanism the mongorestore instance uses to authenticate to the
mongodormongos.Value Description SCRAM-SHA-1 RFC 5802 standard Salted Challenge Response Authentication Mechanism using the SHA1 hash function. MONGODB-CR MongoDB challenge/response authentication. MONGODB-X509 MongoDB TLS/SSL certificate authentication. GSSAPI (Kerberos) External authentication using Kerberos. This mechanism is available only in MongoDB Enterprise. PLAIN (LDAP SASL) External authentication using LDAP. You can also use PLAINfor authenticating in-database users.PLAINtransmits passwords in plain text. This mechanism is available only in MongoDB Enterprise.Note
You cannot specify both
--authenticationMechanismand--uri.
-
--gssapiServiceName¶ New in version 2.6.
Specify the name of the service using GSSAPI/Kerberos. Only required if the service does not use the default name of
mongodb.This option is available only in MongoDB Enterprise.
-
--gssapiHostName¶ New in version 2.6.
Specify the hostname of a service using GSSAPI/Kerberos. Only required if the hostname of a machine does not match the hostname resolved by DNS.
This option is available only in MongoDB Enterprise.
-
--db<database>,-d<database>¶ Specifies the destination database for mongorestore to restore data into when restoring from a BSON file. If the database does not exist, mongorestore creates the database. For example, the following restores the
salariescollection into thereportingdatabase.If you do not specify
--db, mongorestore takes the database name from the data files.The use of
--dband--collectionoptions are deprecated when restoring from a directory or an archive file. Instead, to restore from an archive or a directory, see--nsIncludeinstead.
-
--collection<collection>,-c<collection>¶ Specifies the name of the destination collection for mongorestore to restore data into when restoring from a BSON file. If you do not specify
--collection, mongorestore takes the collection name from the input filename. If the input file has an extension, MongoDB omits the extension of the file from the collection name.The use of
--dband--collectionoptions are deprecated when restoring from a directory or an archive file. Instead, to restore from an archive or a directory, see--nsIncludeinstead.
-
--nsExclude<namespace pattern>¶ New in version 3.4.
Specifies a namespace pattern (e.g.
"test.myCollection","reporting.*","dept*.bar") to exclude the matching namespaces from the restore. In the pattern, you can use asterisks*as wild cards. For an example of the wildcard pattern, see Restore Collections Using Wild Cards.You can specify
--nsExcludemultiple times to exclude multiple namespace patterns.
-
--nsInclude<namespace pattern>¶ New in version 3.4.
Specifies a namespace pattern (e.g.
"test.myCollection","reporting.*","dept*.bar") to restore only the namespaces that match the pattern. In the pattern, you can use asterisks*as wild cards. For an example of the wildcard pattern, see Restore Collections Using Wild Cards.You can specify
--nsIncludemultiple times to include multiple namespace patterns.If source directory or file (i.e. the directory/file from which you are restoring the data) does not contain data files that match the namespace pattern, no data will be restored.
-
--nsFrom<namespace pattern>¶ New in version 3.4.
Use with
--nsToto rename a namespace during the restore operation.--nsFromspecifies the collection in the dump file, while--nsTospecifies the name that should be used in the restored database.--nsFromaccepts a namespace pattern as its argument. The namespace pattern permits--nsFromto refer to any namespace that matches the specified pattern.mongorestorematches the smallest valid occurence of the namespace pattern.For simple replacements, use asterisks (
*) as wild cards. Escape all literal asterisks and backslashes with a backslash. Replacements correspond linearly to matches: each asterisk in--nsFrommust correspond to an asterisk in--nsTo, and the first asterisk in--nsFrommatches the first asterisk innsTo.For more complex replacements, use dollar signs to delimit a “wild card” variable to use in the replacement. Change Collections’ Namespaces during Restore provides an example of complex replacements with dollar sign-delimited wild cards.
Unlike replacements with asterisks, replacements with dollar sign-delimited wild cards do not need to be linear.
-
--nsTo<namespace pattern>¶ New in version 3.4.
Use with
--nsFromto rename a namespace during the restore operation.--nsTospecifies the new collection name to use in the restored database, while--nsFromspecifies the name in the dump file.--nsToaccepts a namespace pattern as its argument. The namespace pattern permits--nsToto refer to any namespace that matches the specified pattern.mongorestorematches the smallest valid occurence of the namespace pattern.For simple replacements, use asterisks (
*) as wild cards. Escape all literal asterisks and backslashes with a backslash. Replacements correspond linearly to matches: each asterisk in--nsFrommust correspond to an asterisk in--nsTo, and the first asterisk in--nsFrommatches the first asterisk innsTo.For more complex replacements, use dollar signs to delimit a “wild card” variable to use in the replacement. Change Collections’ Namespaces during Restore provides an example of complex replacements with dollar sign-delimited wild cards.
Unlike replacements with asterisks, replacements with dollar sign-delimited wild cards do not need to be linear.
-
--objcheck¶ Forces mongorestore to 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.
-
--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.When the restore includes the
admindatabase, mongorestore with--dropremoves all user credentials and replaces them with the users defined in the dump file. Therefore, in systems withauthorizationenabled, mongorestore must be able to authenticate to an existing user and to a user defined in the dump file. If mongorestore can’t authenticate to a user defined in the dump file, the restoration process will fail, leaving an empty database.
-
--dryRun¶ New in version 3.4.
Runs mongorestore without actually importing any data, returning the mongorestore summary information. Use with
--verboseto produce more detailed summary information.
-
--oplogReplay¶ After restoring the database dump, replays the oplog entries from a bson file. When used in conjunction with
mongodump --oplog,mongorestore --oplogReplayrestores the database to the point-in-time backup captured with themongodump --oplogcommand.mongorestore searches for any valid source for the bson file in the following locations:
- The top level of the dump directory, as in the case of a dump created
with
mongodump --oplog. - The path specified by
--oplogFile. <dump-directory>/local/oplog.rs.bson, as in the case of a dump of theoplog.rscollection in thelocaldatabase on amongodthat is a member of a replica set.<dump-directory>/local/oplog.$main.bson, as in the case of a dump of theoplog.rscollection in thelocaldatabase on amongodthat uses master-slave replication.
If there is an
oplog.bsonfile at the top level of the dump directory and a path specified by--oplogFile, mongorestore returns an error.If there is an
oplog.bsonfile at the top level of the dump directory, mongorestore restores that file as the oplog. If there are also bson files in thedump/localdirectory, mongorestore restores them like normal collections.If you specify an oplog file using
--oplogFile, mongorestore restores that file as the oplog. If there are also bson files in thedump/localdirectory, mongorestore restores them like normal collections.If there is both an
oplog.rs.bsonand anoplog.$main.bsonfile in thedump/localdirectory and there is nooplog.bsonfile at the top level of the dump directory or file specified with--oplogFile, mongorestore returns an error.For an example of
--oplogReplay, see Restore Point in Time Oplog Backup.Note
When using
mongorestorewith--oplogReplayto restore a replica set, you must restore a full dump of a replica set member created using~bin.mongodump --oplog.mongorestorewith--oplogReplayfails if you use any of the following options to limit the data be restored:See also
- The top level of the dump directory, as in the case of a dump created
with
-
--oplogLimit<timestamp>¶ Prevents mongorestore from applying oplog entries with timestamp newer than or equal to
<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.
-
--oplogFile<path>¶ New in version 3.4.
Specifies the path to the oplog file containing oplog data for the restore. Use with
--oplogReplay.If you specify
--oplogFileand there is anoplog.bsonfile at the top level of the dump directory, mongorestore returns an error.
-
--keepIndexVersion¶ Prevents mongorestore from upgrading the index to the latest version during the restoration process.
-
--noIndexRestore¶ Prevents mongorestore from restoring and building indexes as specified in the corresponding
mongodumpoutput.
-
--noOptionsRestore¶ Prevents mongorestore from setting the collection options, such as those specified by the
collModdatabase command, on restored collections.
-
--restoreDbUsersAndRoles¶ Restore user and role definitions for the given database. See system.roles Collection and system.users Collection for more information.
-
--writeConcern<document>¶ Default: majority
Specifies the write concern for each write operation that mongorestore performs.
Specify the write concern as a document with w options. For example:
-
--maintainInsertionOrder¶ Default: false
If specified, mongorestore inserts the documents in the order of their appearance in the input source, otherwise mongorestore may perform the insertions in an arbitrary order.
-
--numParallelCollectionsint,-jint¶ Default: 4
Number of collections mongorestore should restore in parallel.
If you specify
-jwhen restoring a single collection,-jmaps to the--numInsertionWorkersPerCollectionoption rather than--numParallelCollections.
-
--numInsertionWorkersPerCollectionint¶ Default: 1
New in version 3.0.0.
Specifies the number of insertion workers to run concurrently per collection.
For large imports, increasing the number of insertion workers may increase the speed of the import.
-
--stopOnError¶ New in version 3.0.
Forces mongorestore to halt the restore when it encounters an error.
-
--bypassDocumentValidation¶ Enables mongorestore to bypass document validation during the operation. This lets you insert documents that do not meet the validation requirements.
New in version 3.2.1.
-
--gzip¶ New in version 3.2.
Restores from compressed files or data stream created by
~bin.mongodump --gzipTo restore from a dump directory that contains compressed files, run
mongorestorewith the--gzipoption.To restore from a compressed archive file, run
mongorestorewith both the--gzipand the –archive options.
-
--archive<=file|null>¶ New in version 3.2.
Restores from an archive file or from the standard input (
stdin).To restore from an archive file, run mongorestore with the
--archiveoption and the archive filename.To restore from the standard input, run mongorestore with the
--archiveoption but omit the filename.
-
<path>¶ The directory path or BSON file name from which to restore data.
You cannot specify both the
<path>argument and the--diroption, which also specifies the dump directory, to mongorestore.
-
--dirstring¶ Specifies the dump directory.
- You cannot specify both the
--diroption and the<path>argument, which also specifies the dump directory, to mongorestore. - You cannot use the
--archiveoption with the--diroption.
- You cannot specify both the
Examples¶
Restore with Access Control¶
In the following example, mongorestore restores from
/opt/backup/mongodump-2011-10-24 to a mongod
instance running on port 27017 on the host
mongodb1.example.net. The --uri
string omits the user’s password to have mongorestore
prompt for the password.
Alternatively, you can specify the host, port, username, and
authentication database using --host,
--port, --username, and --authenticationDatabase. Omit --password to have mongorestore prompt for the password:
Restore a Collection¶
New in version 3.4.
To restore a specific collection, use --nsInclude, passing in the full namespace
(<database>.<collection>) of the collection.
For example, the following restores the collection named
purchaseorders in the database test from the corresponding
files located in the dump/ directory.
The mongorestore outputs the results, including the
number of documents restored:
If the dump/ directory does not contain the corresponding data
files for the specified namespace, no data will be restored:
Alternatively, you can restore a specific collection using the
--db, --collection, and a .bson file:
Restore Collections Using Wild Cards¶
New in version 3.4.
--nsInclude and
--nsExclude support specifying the
namespaces you wish to include or exclude from a
restore operation using asterisks as wild cards.
The following example restores the documents in the dump/
sub-directory of the current directory that match the specified
namespace pattern. The --nsInclude
statement specifies to only restore documents in the transactions
database while --nsExclude
instructs mongorestore to exclude collections whose
names end with _dev. mongorestore restores data to
the mongod instance running on the localhost interface
on port 27017.
Change Collections’ Namespaces during Restore¶
New in version 3.4.
MongoDB 3.4 added the --nsFrom and
--nsTo options, which enable you to
change the namespace of a collection that you are restoring.
--nsFrom and --nsTo support using asterisks as wild cards and
support using dollar signs to delimit “wild card” variables to use in
the replacement.
Consider a database data that you have exported to a dump/
directory using mongodump. The data database
contains the following collections:
sales_customer1sales_customer2sales_customer3users_customer1users_customer2users_customer3
Using --nsFrom and --nsTo, you can restore the data into different
namespaces. The following operation
- restores the
sales_<customerName>collections in thedatadatabase tosalescollections in the<customerName>database, and - restores the
users_<customerName>collections touserscollections in the<customerName>database.
Restore from an Archive File¶
To restore from an archive file, run restore with the new
--archive option and the archive filename.
Restore a Database from an Archive File¶
New in version 3.2.
To restore from an archive file, run restore with the new
--archive option and the archive filename. For example, the
following operation restores the test database from the file
test.20150715.archive.
Restore from Compressed Data¶
New in version 3.2: With the --gzip option, mongorestore can restore from
compressed files or data stream created by mongodump.
To restore from a dump directory that contains compressed files, run
mongorestore with the --gzip. For example, the following operation restores the test
database from the compressed files located in the default dump
directory:
To restore from a compressed archive file, run
mongorestore with the --gzip option and the --archive
option. For example, the following operation restores the test
database from the archive file test.20150715.gz.
Restore a Database from Standard Input¶
New in version 3.2.
To restore from the standard input, run mongorestore
with the --archive option but omit
the filename. For example: