On this page
mongoexport
On this page
macOS Sierra and Go 1.6 Incompatibility
Users running on macOS Sierra require the 3.2.10 or newer version of mongoexport.
Synopsis
mongoexport
is a utility that produces a JSON or CSV export of data stored in a MongoDB instance.
See the mongoimport document for more information regarding the mongoimport
utility, which provides the inverse “importing” capability.
Run mongoexport
from the system command line, not the mongo
shell.
Considerations
Warning
Avoid using mongoimport
and mongoexport
for full instance production backups. They do not reliably preserve all rich BSON data types, because JSON can only represent a subset of the types supported by BSON. Use mongodump
and mongorestore
as described in MongoDB Backup Methods for this kind of functionality.
mongoexport
must be run directly from the system command line.
To preserve type information, mongoexport
and mongoimport
uses the strict mode representation for certain types.
For example, the following insert operation in the mongo
shell uses the shell mode representation for the BSON types data_date
and data_numberlong
:
use test
db.traffic.insert( { _id: 1, volume: NumberLong('2980000'), date: new Date() } )
The argument to data_numberlong
must be quoted to avoid potential loss of accuracy.
Use mongoexport
to export the data:
mongoexport --db test --collection traffic --out traffic.json
The exported data is in strict mode representation to preserve type information:
{ "_id" : 1, "volume" : { "$numberLong" : "2980000" }, "date" : { "$date" : "2014-03-13T13:47:42.483-0400" } }
See MongoDB Extended JSON for a complete list of these types and the representations used.
Required Access
mongoexport
requires read access on the target database.
Ensure that the connecting user possesses, at a minimum, the read
role on the target database.
When connecting to a mongod
or mongos
that enforces Authentication, ensure you use the required security parameters based on the configured authentication mechanism.
Read Preference
mongoexport
defaults to primary
read preference when connected to a mongos
or a replica set.
You can override the default read preference using the --readPreference
option.
Important
Using a non-primary read preference on a mongos
may produce inconsistencies in data, including duplicates or missing documents.
Options
Changed in version 3.0.0: mongoexport
removed the --dbpath
as well as related --directoryperdb
and --journal
options. To use mongoexport
, you must run mongoexport
against a running mongod
or mongos
instance as appropriate.
Changed in version 3.0.0: mongoexport
removed the --csv
option. Use the --type=csv
option to specify CSV format for the output.
--verbose
,
-v
-
Increases the amount of internal reporting returned on standard output or in log files. Increase the verbosity with the
-v
form by including the option multiple times, (e.g.-vvvvv
.)
--quiet
-
Runs mongoexport 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
--uri
<connectionString>
-
New in version 3.4.6.
Specify a resolvable URI connection string to connect to the MongoDB deployment.
--uri "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]"
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
--uri
option:--host
--port
--db
--username
--password
(if the URI connection string also includes the password)--authenticationDatabase
--authenticationMechanism
Instead, specify these options as part of your
--uri
connection string.
--host
<hostname><:port>
,
-h
<hostname><:port>
-
Default: localhost:27017
Specifies a resolvable hostname for the
mongod
to which to connect. By default, the mongoexport attempts to connect to a MongoDB instance running on the localhost on port number27017
.To connect to a replica set, specify the
replSetName
and a seed list of set members, as in the following:--host <replSetName>/<hostname1><:port>,<hostname2><:port>,<...>
When specifying the replica set list format, mongoexport 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:
--host <hostname1><:port>
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 mongoexport to connect to the MongoDB instance using an IPv6 network. Prior to MongoDB 3.0, you had to specify
--ipv6
to use IPv6. In MongoDB 3.0 and later, IPv6 is always enabled.
--ssl
-
New in version 2.6.
Enables connection to a
mongod
ormongos
that 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
.pem
file that contains the root certificate chain from the Certificate Authority. Specify the file name of the.pem
file using relative or absolute paths.Starting in version 3.4, if
--sslCAFile
orssl.CAFile
is 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,
--sslCAFile
orssl.CAFile
must be specified.Warning
Version 3.2 and earlier: For TLS/SSL connections (
--ssl
) tomongod
andmongos
, if the mongoexport runs without the--sslCAFile
, mongoexport will not attempt to validate the server certificates. This creates a vulnerability to expiredmongod
andmongos
certificates as well as to foreign processes posing as validmongod
ormongos
instances. Ensure that you always specify the CA file to validate