- Reference >
- MongoDB Package Components >
mongodump
mongodump¶
On this page
Mac OSX Sierra and Go 1.6 Incompatibility
Users running on Mac OSX Sierra require the 3.2.10 or newer version of mongodump.
Synopsis¶
mongodump is a utility for creating a binary export of the
contents of a database. mongodump can export data from
either mongod or mongos instances.
mongodump can be a part of a backup strategy with mongorestore for partial
backups based on a query, syncing from production to staging or
development environments, or changing the storage engine of a
standalone. However, the use of mongodump and
mongorestore as a backup strategy can be problematic for
sharded clusters and replica sets.
Run mongodump from the system command line, not the mongo shell.
For an overview of mongodump in conjunction with
mongorestore part of a backup and recovery strategy, see
Back Up and Restore with MongoDB Tools.
Behavior¶
Data Exclusion¶
mongodump excludes the content of the local database in its output.
mongodump only captures the documents in the database in its
backup data and does not include index data. mongorestore or
mongod must then rebuild the indexes after restoring data.
Changed in version 3.4: MongoDB 3.4 added support for
read-only views. By default,
mongodump only captures a view’s metadata: it does not
create a binary export of the documents included in the view. To
capture the documents in a view use --viewsAsCollections.
Read Preference¶
Replica Sets¶
Changed in version 3.2.0: The choice of target or targets for the --host
parameter affects the read preference of mongodump
when connecting to a replica set.
If the string passed to
--hostis prefixed by the replica set name,mongodumpreads from theprimaryreplica set member by default. For example:--host "replSet/rep1.example.net:27017,rep2.example.net:27017,rep3.example.net:27017"
If the string passed to
--hostcontains a list ofmongodinstances, but does not include the replica set name as a prefix to the host string,mongodumpreads from thenearestnode by default. For example:--host "rep1.example.net:27017,rep2.example.net:27017,rep3.example.net:27017"
Sharded Clusters¶
Changed in version 3.0.5: For a sharded cluster where the shards are replica sets,
mongodump no longer prefers reads from secondary
members when run against the mongos instance.
For sharded clusters, specify the hostname of a mongos
for the --host option.
mongodump reads from the primary replica set
member on each shard replica set in the cluster.
Unsharded collections in a sharded cluster are stored on a
primary shard.
mongodump reads from the primary replica set member on
the primary shard for any unsharded collections. Note: each
database has its own primary shard.
Overwrite Files¶
mongodump overwrites output files if they exist in the
backup data folder. Before running the mongodump command
multiple times, either ensure that you no longer need the files in the
output folder (the default is the dump/ folder) or rename the
folders or files.
Data Compression Handling¶
When run against a mongod instance that uses the
WiredTiger storage engine,
mongodump outputs uncompressed data.
Required Access¶
To run mongodump against a MongoDB deployment that has
access control enabled, you must have
privileges that grant find action for each database to
back up. The built-in backup role provides the required
privileges to perform backup of any and all databases.
Changed in version 3.2.1: The backup role provides additional privileges to back
up the system.profile
collections that exist when running with database profiling. Previously, users required an additional
read access on this collection.
Options¶
Changed in version 3.0.0: mongodump removed the --dbpath as well as related
--directoryperdb and --journal options. To use
mongodump, you must run mongodump against a running
mongod or mongos instance as appropriate.
-
mongodump¶
-
mongodump¶
-
--help¶ Returns information on the options and use of
mongodump.
-
--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
mongodumpin 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
mongodumprelease number.
-
--host<hostname><:port>,-h<hostname><:port>¶ Default: localhost:27017
Specifies a resolvable hostname for the
mongodto which to connect. By default, themongodumpattempts 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:<replSetName>/<hostname1><:port>,<hostname2><:port>,<...>
You can always connect directly to a single MongoDB instance by specifying the host and port number directly.
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.
-
--ipv6¶ Removed in version 3.0.
Enables IPv6 support and allows
mongodumpto connect to the MongoDB instance using an IPv6 network. Prior to MongoDB 3.0, you had to specify--ipv6to use IPv6. In MongoDB 3.0 and later, IPv6 is always enabled.
-
--ssl¶ New in version 2.6.
Enables connection to a
mongodormongosthat has TLS/SSL support enabled.Changed in version 3.0: Most MongoDB distributions include support for TLS/SSL. See Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients for more information about TLS/SSL and MongoDB.
Changed in version 3.4: If
--sslCAFileis not specified when connecting to an TLS/SSL-enabled server, the system-wide CA certificate store will be used.
-
--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.Changed in version 3.0: Most MongoDB distributions include support for TLS/SSL. See Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients for more information about TLS/SSL and MongoDB.
Changed in version 3.4: If
--sslCAFileis not specified when connecting to an TLS/SSL-enabled server, the system-wide CA certificate store will be used.Warning
Version 3.2 and earlier: For SSL connections (
--ssl) tomongodandmongos, if themongodumpruns without the--sslCAFile,mongodumpwill 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.
-
--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.Changed in version 3.0: Most MongoDB distributions include support for TLS/SSL. See Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients for more information about TLS/SSL and MongoDB.
Changed in version 3.4: If
--sslCAFileis not specified when connecting to an TLS/SSL-enabled server, the system-wide CA certificate store will be used.
-
--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, themongodumpwill 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, themongodumpwill prompt for a passphrase. See SSL Certificate Passphrase.Changed in version 3.0: Most MongoDB distributions include support for TLS/SSL. See Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients for more information about TLS/SSL and MongoDB.
Changed in version 3.4: If
--sslCAFileis not specified when connecting to an TLS/SSL-enabled server, the system-wide CA certificate store will be used.
-
--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.Changed in version 3.0: Most MongoDB distributions include support for TLS/SSL. See Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients for more information about TLS/SSL and MongoDB.
Changed in version 3.4: If
--sslCAFileis not specified when connecting to an TLS/SSL-enabled server, the system-wide CA certificate store will be used.
-
--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.Changed in version 3.0: Most MongoDB distributions include support for TLS/SSL. See Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients for more information about TLS/SSL and MongoDB.
Changed in version 3.4: If
--sslCAFileis not specified when connecting to an TLS/SSL-enabled server, the system-wide CA certificate store will be used.
-
--sslAllowInvalidHostnames¶ New in version 3.0.
Disables the validation of the hostnames in TLS/SSL certificates. Allows
mongodumpto connect to MongoDB instances even if the hostname in their certificates do not match the specified hostname.Changed in version 3.0: Most MongoDB distributions include support for TLS/SSL. See Configure mongod and mongos for TLS/SSL and TLS/SSL Configuration for Clients for more information about TLS/SSL and MongoDB.
Changed in version 3.4: If
--sslCAFileis not specified when connecting to an TLS/SSL-enabled server, the system-wide CA certificate store will be used.
-
--sslFIPSMode¶ New in version 2.6.
Directs the
mongodumpto 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 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.
-
--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.0: If you do not specify an argument for
--password,mongodumpreturns an error.Changed in version 3.0.2: If you wish
mongodumpto prompt the user for the password, pass the--usernameoption without--passwordor specify an empty string as the--passwordvalue, as in--password "".
-
--authenticationDatabase<dbname>¶ Specifies the database in which the user is created. See Authentication Database.
If you do not specify an authentication database,
mongodumpassumes that the database specified to export holds the user’s credentials.If you do not specify an authentication database or a database to export,
mongodumpassumes theadmindatabase holds the user’s credentials.
-
--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
mongodumpinstance uses to authenticate to themongodormongos.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.
-
--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 a database to backup. If you do not specify a database,
mongodumpcopies all databases in this instance into the dump files.
-
--collection<collection>,-c<collection>¶ Specifies a collection to backup. If you do not specify a collection, this option copies all collections in the specified database or instance to the dump files.
-
--query<json>,-q<json>¶ Provides a JSON document as a query that optionally limits the documents included in the output of
mongodump.You must enclose the query in single quotes (e.g.
') to ensure that it does not interact with your shell environment.
-
--queryFile<path>¶ New in version 3.2.
Specifies the path to a file containing a JSON document as a query filter that limits the documents included in the output of
mongodump.--queryFileenables you to create query filters that are too large to fit in your terminal’s buffer.
-
--readPreference<string>¶ Specify the read preference for
mongodump.mongodumpdefaults toprimaryread preference when connected to amongosor a replica set.Otherwise,
mongodumpdefaults tonearest.Warning
Using a read preference other than
primarywith a connection to amongosmay produce inconsistencies, duplicates, or result in missed documents.See Read Preference.
-
--forceTableScan¶ Forces
mongodumpto scan the data store directly: typically,mongodumpsaves entries as they appear in the index of the_idfield. If you specify a query--query,mongodumpwill use the most appropriate index to support that query.Use
--forceTableScanto skip the index and scan the data directly. Typically there are two cases where this behavior is preferable to the default:- If you have key sizes over 800 bytes that would not be present in the
_idindex. - Your database uses a custom
_idfield.
When you run with
--forceTableScan,mongodumpdoes not use$snapshot. As a result, the dump produced bymongodumpcan reflect the state of the database at many different points in time.Important
Use
--forceTableScanwith extreme caution and consideration.- If you have key sizes over 800 bytes that would not be present in the
-
--gzip¶ New in version 3.2.
Compresses the output. If
mongodumpoutputs to the dump directory, the new feature compresses the individual files. The files have the suffix.gz.If
mongodumpoutputs to an archive file or the standard out stream, the new feature compresses the archive file or the data output to the stream.
-
--out<path>,-o<path>¶ Specifies the directory where
mongodumpwill write BSON files for the dumped databases. By default,mongodumpsaves output files in a directory nameddumpin the current working directory.To send the database dump to standard output, specify “
-” instead of a path. Write to standard output if you want process the output before saving it, such as to usegzipto compress the dump. When writing standard output,mongodumpdoes not write the metadata that writes in a<dbname>.metadata.jsonfile when writing to files directly.You cannot use the
--archiveoption with the--outoption.
-
--archive<file or null>¶ New in version 3.2.
Writes the output to a single archive file or to the standard output (
stdout).To output the dump to an archive file, run
mongodumpwith the new--archiveoption and the archive filename.To output the dump to the standard output stream in order to pipe to another process, run
mongodumpwith thearchiveoption but omit the filename.You cannot use the
--archiveoption with the--outoption.
-
--repair¶ Runs a repair option in addition to dumping the database. The repair option changes the behavior of
mongodumpto only write valid data and exclude data that may be in an invalid state as a result of an improper shutdown ormongodcrash.The
--repairoption uses aggressive data-recovery algorithms that may produce a large amount of duplication.--repairis only available for use withmongodinstances using themmapv1storage engine. You cannot run--repairwithmongosor withmongodinstances that use thewiredTigerstorage engine. To repair data in amongodinstance usingwiredTigerusemongod --repair.
-
--oplog¶ Creates a file named
oplog.bsonas part of themongodumpoutput. Theoplog.bsonfile, located in the top level of the output directory, contains oplog entries that occur during themongodumpoperation. This file provides an effective point-in-time snapshot of the state of amongodinstance. To restore to a specific point-in-time backup, use the output created with this option in conjunction withmongorestore --oplogReplay.Without
--oplog, if there are write operations during the dump operation, the dump will not reflect a single moment in time. Changes made to the database during the update process can affect the output of the backup.--oploghas no effect when runningmongodumpagainst amongosinstance to dump the entire contents of a sharded cluster. However, you can use--oplogto dump individual shards.--oplogonly works against nodes that maintain an oplog. This includes all members of a replica set, as well as master nodes in master/slave replication deployments.--oplogdoes not dump the oplog collection.
-
--dumpDbUsersAndRoles¶ Includes user and role definitions in the database’s dump directory when performing
mongodumpon a specific database. This option applies only when you specify a database in the--dboption. MongoDB always includes user and role definitions whenmongodumpapplies to an entire instance and not just a specific database.
-
--excludeCollectionstring¶ New in version 3.0.
Excludes the specified collection from the
mongodumpoutput. To exclude multiple collections, specify the--excludeCollectionmultiple times.
-
--excludeCollectionsWithPrefixstring¶ New in version 3.0.
Excludes all collections with a specified prefix from the
mongodumpoutputs. To specify multiple prefixes, specify the--excludeCollectionsWithPrefixmultiple times.
-
--numParallelCollectionsint,-jint¶ Default: 4
Number of collections
mongodumpshould export in parallel.
-
--viewsAsCollections¶ New in version 3.4.
When specified,
mongodumpexports read-only views as collections. For each view,mongodumpwill produce a BSON file containing the documents in the view. If youmongorestorethe produced BSON file, the view will be restored as a collection.If you do not include
--viewsAsCollections,mongodumpcaptures each view’s metadata. If you include a view’s metadata file in amongorestoreoperation, the view is recreated.
Examples¶
mongodump a Collection¶
The following operation creates a dump file that contains only the
collection named collection in the database named test. In
this case the database is running on the local interface on port
27017:
mongodump --db test --collection collection
mongodump a Database Excluding Specified Collections¶
The following operation dumps all collections in the test database
except for users and salaries:
mongodump --db test --excludeCollection=users --excludeCollection=salaries
mongodump with Access Control¶
In the next example, mongodump creates a database dump
located at /opt/backup/mongodump-2011-10-24, from a database
running on port 37017 on the host mongodb1.example.net and
authenticating using the username user and the password
pass, as follows:
mongodump --host mongodb1.example.net --port 37017 --username user --password "pass" --out /opt/backup/mongodump-2011-10-24
Output to an Archive File¶
New in version 3.2.
To output the dump to an archive file, run mongodump with the
--archive option and the archive filename. For example, the following
operation creates a file test.20150715.archive that contains the dump
of the test database.
mongodump --archive=test.20150715.archive --db test
Output an Archive to Standard Output¶
New in version 3.2.
To output the archive to the standard output stream in order to pipe to
another process, run mongodump with the archive
option but omit the filename:
mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018
Note
You cannot use the --archive option with the
--out option.
Compress the Output¶
To compress the files in the output dump directory, run
mongodump with the new --gzip option. For example,
the following operation outputs compressed files into the default
dump directory.
mongodump --gzip --db test
To compress the archive file output by mongodump, use the
--gzip option in conjunction with the --archive
option, specifying the name of the compressed file.
mongodump --archive=test.20150715.gz --gzip --db test