mongoexport (1) - Linux Manuals

mongoexport: MongoDB Export Utility

NAME

mongoexport - MongoDB Export Utility

SYNOPSIS

mongoexport is a utility that produces a JSON or CSV export of data stored in a MongoDB instance. See the http://docs.mongodb.org/manual/core/import-export document for a more in depth usage overview, and the mongoimport document for more information regarding the mongoimport utility, which provides the inverse "importing" capability.

CONSIDERATIONS

Do not use mongoimport and mongoexport for full-scale production backups because they may not reliably capture data type information. Use mongodump and mongorestore as described in http://docs.mongodb.org/manual/core/backups for this kind of functionality.

OPTIONS

mongoexport
mongoexport
--help, -h
Returns information on mongoexport options and usage.
--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
--version
Returns the mongoexport release number.
--host <hostname><:port>, -h
Specifies a resolvable hostname for the mongod to which to connect. By default mongoexport attempts to connect to a MongoDB instance running on the localhost on port number 27017.

To connect to a replica set, specify the replica set seed name and the seed list of set members. Use the following format:

<replica_set_name>/<hostname1><:port>,<hostname2:<port>,...

You can always connect directly to a single MongoDB instance by specifying the host and port number directly.

--port <port>
Specifies the port number when the MongoDB instance is not running on the standard port of 27017. You may also specify the port number using the --host option.
--ipv6
Enables IPv6 support, which allows mongoexport to connect to the MongoDB instance using an IPv6 network. All MongoDB programs and processes, including mongoexport, disable IPv6 support by default.
--ssl
New in version 2.6.

Enables connection to a mongod or mongos that has SSL support enabled.

The default distribution of MongoDB does not contain support for SSL. For more information on MongoDB and SSL, see http://docs.mongodb.org/manual/tutorial/configure-ssl.

--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.

The default distribution of MongoDB does not contain support for SSL. For more information on MongoDB and SSL, see http://docs.mongodb.org/manual/tutorial/configure-ssl.

--sslPEMKeyFile <filename>
New in version 2.6.

Specifies the .pem file that contains both the SSL certificate and key. Specify the file name of the .pem file using relative or absolute paths.

This option is required when using the --ssl option to connect to a mongod or mongos that has sslCAFile enabled without sslWeakCertificateValidation.

The default distribution of MongoDB does not contain support for SSL. For more information on MongoDB and SSL, see http://docs.mongodb.org/manual/tutorial/configure-ssl.

--sslPEMKeyPassword <value>
New in version 2.6.

Specifies the password to de-crypt the certificate-key file (i.e. --sslPEMKeyFile). Use --sslPEMKeyPassword only if the certificate-key file is encrypted. In all cases, mongoexport 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 --sslPEMKeyPassword, mongoexport will prompt for a passphrase. See ssl-certificate-password.

The default distribution of MongoDB does not contain support for SSL. For more information on MongoDB and SSL, see http://docs.mongodb.org/manual/tutorial/configure-ssl.

--sslCRLFile <filename>
New in version 2.6.

Specifies the .pem file that contains the Certificate Revocation List. Specify the file name of the .pem file using relative or absolute paths.

The default distribution of MongoDB does not contain support for SSL. For more information on MongoDB and SSL, see http://docs.mongodb.org/manual/tutorial/configure-ssl.

--sslAllowInvalidCertificates
New in version 2.6.

Bypasses the validation checks for server certificates and allows the use of invalid certificates. When using the sslAllowInvalidCertificates setting, MongoDB logs as a warning the use of the invalid certificate.

The default distribution of MongoDB does not contain support for SSL. For more information on MongoDB and SSL, see http://docs.mongodb.org/manual/tutorial/configure-ssl.

--sslFIPSMode
New in version 2.6.

Directs mongoexport to use the FIPS mode of the installed OpenSSL library. Your system must have a FIPS compliant OpenSSL library to use --sslFIPSMode.

The default distribution of MongoDB does not contain support for SSL. For more information on MongoDB and SSL, see http://docs.mongodb.org/manual/tutorial/configure-ssl.

--username <username>, -u
Specifies a username with which to authenticate to a MongoDB database that uses authentication. Use in conjunction with the --password and --authenticationDatabase options.
--password <password>, -p
Specifies a password with which to authenticate to a MongoDB database that uses authentication. Use in conjunction with the --username and --authenticationDatabase options.
--authenticationDatabase <dbname>
New in version 2.4.

Specifies the database that holds the user's credentials. If you do not specify an authentication database, mongoexport assumes that the database specified as the argument to the --db option holds the user's credentials.

--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, mongoexport also includes support for GSSAPI to handle Kerberos authentication. See http://docs.mongodb.org/manual/tutorial/control-access-to-mongodb-with-kerberos-authentication for more information about Kerberos authentication.

--dbpath <path>
Specifies the directory of the MongoDB data files. If used, the --dbpath option enables mongoexport to attach directly to local data files without a running mongod. When run with --dbpath, mongoexport locks access to the data directory. No mongod can access the same path while the process runs.
--directoryperdb
When used in conjunction with the corresponding option in mongod, allows mongoexport to export data from MongoDB instances that have every database's files saved in discrete directories on the disk. This option is only relevant when specifying the --dbpath option.
--journal
Allows mongoexport operations to use the durability journal to ensure data files remain valid and recoverable. This option is only relevant when specifying the --dbpath option.
--db <database>, -d
Specifies the name of the database on which to run mongoexport.
--collection <collection>, -c
Specifies the collection to export.
--fields <field1[,field2]>, -f
Specifies a field or fields to include in the export. Use a comma separated list of fields to specify multiple fields.

For --csv output formats, mongoexport includes only the specified field(s), and the specified field(s) can be a field within a sub-document.

For JSON output formats, mongoexport includes only the specified field(s) and the _id field, and if the specified field(s) is a field within a sub-document, the mongoexport includes the sub-document with all its fields, not just the specified field within the document.

--fieldFile <filename>
As an alternative to --fields, the --fieldFile option allows you to specify in a file the field or fields to include in the export and is only valid with the --csv option. The file must have only one field per line, and the line(s) must end with the LF character (0x0A).

mongoexport includes only the specified field(s). The specified field(s) can be a field within a sub-document.

--query <JSON>, -q
Provides a JSON document as a query that optionally limits the documents returned in the export. Specify JSON in strict format.

For example, given a collection named records in the database test with the following documents:

{ "_id" : ObjectId("51f0188846a64a1ed98fde7c"), "a" : 1 }
{ "_id" : ObjectId("520e61b0c6646578e3661b59"), "a" : 1, "b" : 2 }
{ "_id" : ObjectId("520e642bb7fa4ea22d6b1871"), "a" : 2, "b" : 3, "c" : 5 }
{ "_id" : ObjectId("520e6431b7fa4ea22d6b1872"), "a" : 3, "b" : 3, "c" : 6 }
{ "_id" : ObjectId("520e6445b7fa4ea22d6b1873"), "a" : 5, "b" : 6, "c" : 8 }

The following mongoexport uses the -q option to export only the documents with the field a greater than or equal to ($gte) to 3:

mongoexport -d test -c records -q "{ a: { \$gte: 3 } } }" --out exportdir/myRecords.json

The resulting file contains the following documents:

{ "_id" : { "$oid" : "520e6431b7fa4ea22d6b1872" }, "a" : 3, "b" : 3, "c" : 6 }
{ "_id" : { "$oid" : "520e6445b7fa4ea22d6b1873" }, "a" : 5, "b" : 6, "c" : 8 }

You can sort the results with the --sort option to mongoexport.

--csv
Changes the export format to a comma-separated-values (CSV) format. By default mongoexport writes data using one JSON document for every MongoDB document.

If you specify --csv, then you must also use either the --fields or the --fieldFile option to declare the fields to export from the collection.

--out <file>, -o
Specifies a file to write the export to. If you do not specify a file name, the mongoexport writes data to standard output (e.g. stdout).
--jsonArray
Modifies the output of mongoexport to write the entire contents of the export as a single JSON array. By default mongoexport writes data using one JSON document for every MongoDB document.
--slaveOk, -k
Allows mongoexport to read data from secondary or slave nodes when using mongoexport with a replica set. This option is only available if connected to a mongod or mongos and is not available when used with the "mongoexport --dbpath" option.

This is the default behavior.

--forceTableScan
New in version 2.2.

Forces mongoexport to scan the data store directly: typically, mongoexport saves entries as they appear in the index of the _id field. Use --forceTableScan to skip the index and scan the data directly. Typically there are two cases where this behavior is preferable to the default:

1.
If you have key sizes over 800 bytes that would not be present in the _id index.
2.
Your database uses a custom _id field.

When you run with --forceTableScan, mongoexport does not use $snapshot. As a result, the export produced by mongoexport can reflect the state of the database at many different points in time.

WARNING: Use --forceTableScan with extreme caution and consideration.

--skip <number>
Use --skip to control where mongoexport begins exporting documents. See skip() for information about the underlying operation.
--limit <number>
Specifies a maximum number of documents to include in the export. See limit() for information about the underlying operation.
--sort <JSON>
Specifies an ordering for exported results. If an index does not exist that can support the sort operation, the results must be less than 32 megabytes.

Use --sort conjunction with --skip and --limit to limit number of exported documents.

mongoexport -d test -c records --sort '{a: 1}' --limit 100 --out export.0.json
mongoexport -d test -c records --sort '{a: 1}' --limit 100 --skip 100 --out export.1.json
mongoexport -d test -c records --sort '{a: 1}' --limit 100 --skip 200 --out export.2.json

See sort() for information about the underlying operation.

USE

Export in CSV Format

In the following example, mongoexport exports the collection contacts from the users database from the mongod instance running on the localhost port number 27017. This command writes the export data in CSV format into a file located at /opt/backups/contacts.csv. The fields.txt file contains a line-separated list of fields to export.

mongoexport --db users --collection contacts --csv --fieldFile fields.txt --out /opt/backups/contacts.csv

Export in JSON Format

The next example creates an export of the collection contacts from the MongoDB instance running on the localhost port number 27017, with journaling explicitly enabled. This writes the export to the contacts.json file in JSON format.

mongoexport --db sales --collection contacts --out contacts.json --journal

Export Collection Directly From Data Files

The following example exports the collection contacts from the sales database located in the MongoDB data files located at /srv/mongodb/. This operation writes the export to standard output in JSON format.

mongoexport --db sales --collection contacts --dbpath /srv/mongodb/

WARNING: The above example will only succeed if there is no mongod connected to the data files located in the /srv/mongodb/ directory.

Export from Remote Host Running with Authentication

The following example exports the collection contacts from the database marketing . This data resides on the MongoDB instance located on the host mongodb1.example.net running on port 37017, which requires the username user and the password pass.

mongoexport --host mongodb1.example.net --port 37017 --username user --password pass --collection contacts --db marketing --out mdb1-examplenet.json

TYPE FIDELITY

WARNING: mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity. See http://docs.mongodb.org/manual/reference/mongodb-extended-json for more information.

JSON can only represent a subset of the types supported by BSON. To preserve type information, mongoexport uses the strict mode representation for certain types.

For example, the following insert operation in the mongo shell uses the mongoShell mode representation for the BSON types data_date and data_numberlong:

use test
db.traffic.insert( { _id: 1, volume: NumberLong(2980000), date: new Date() } )

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 http://docs.mongodb.org/manual/reference/mongodb-extended-json for a complete list of these types and the representations used.

AUTHOR

MongoDB Documentation Project

COPYRIGHT

2011-2014, MongoDB, Inc.