- Reference >
- MongoDB Package Components >
mongoexport
mongoexport¶
Synopsis¶
mongoexport is a utility that produces a JSON or CSV export
of data stored in a MongoDB instance. See the
Import and Export MongoDB Data 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.
Note
Do not use mongoimport and mongoexport for
full-scale backups because they may not reliably capture data type
information. Use mongodump and mongorestore as
described in MongoDB Backup Methods for this kind of
functionality.
Options¶
-
mongoexport¶
-
--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
mongoexportutility.
-
--host<hostname><:port>¶ Specifies a resolvable hostname for the
mongodfrom which you want to export data. By defaultmongoexportattempts to connect to a MongoDB process running on the localhost port number27017.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 themongoexport --hostcommand.
-
--ipv6¶ Enables IPv6 support that allows
mongoexportto connect to the MongoDB instance using an IPv6 network. All MongoDB programs and processes, includingmongoexport, disable IPv6 support by default.
-
--ssl¶ New in version 2.4: MongoDB added support for SSL connections to
mongodinstances in mongoexport.Note
SSL support in mongoexport is not compiled into the default distribution of MongoDB. See Connect to MongoDB with SSL for more information on SSL and MongoDB.
Additionally, mongoexport does not support connections to
mongodinstances that require client certificate validation.Allows
mongoexportto 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
mongoexport --passwordoption to supply a password.
-
--password<password>,-p<password>¶ Specifies a password to authenticate to the MongoDB instance. Use in conjunction with the
--usernameoption to supply a username.If you specify a
--usernameand do not pass an argument to--password,mongoexportwill 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,
mongoexportassumes 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,mongoexportalso 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 enablesmongoexportto attach directly to local data files and insert the data without themongod. To run with--dbpath,mongoexportneeds to lock access to the data directory: as a result, nomongodcan access the same path while the process runs.
-
--directoryperdb¶ Use the
--directoryperdbin conjunction with the corresponding option tomongod, which allowsmongoexportto 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--dbpathoption.
-
--journal¶ Allows
mongoexportoperations to access the durability journal to ensure that the export is in a valid state. This option is only relevant when specifying the--dbpathoption.
-
--db<db>,-d<db>¶ Use the
--dboption to specify the name of the database that contains the collection you want to export.
-
--collection<collection>,-c<collection>¶ Use the
--collectionoption to specify the collection that you wantmongoexportto export.
-
--fields<field1[,field2]>,-f<field1[,field2]>¶ Specify a field or fields to include in the export. Use a comma separated list of fields to specify multiple fields.
For
--csvoutput formats,mongoexportincludes only the specified field(s), and the specified field(s) can be a field within a sub-document.For JSON output formats,
mongoexportincludes only the specified field(s) and the_idfield, and if the specified field(s) is a field within a sub-document, themongoexportincludes the sub-document with all its fields, not just the specified field within the document.
-
--fieldFile<file>¶ As an alternative to
--fields, the--fieldFileoption allows you to specify in a file the field or fields to include in the export and is only valid with the--csvoption. The file must have only one field per line, and the line(s) must end with the LF character (0x0A).mongoexportincludes only the specified field(s). The specified field(s) can be a field within a sub-document.
-
--query<JSON>,-q<JSON>¶ Provides a JSON document as a query that optionally limits the documents returned in the export.
Example
Given a collection named
recordsin the databasetestwith 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
mongoexportuses the-qoption to export only the documents with the fieldagreater than or equal to ($gte) to3: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 }
-
--csv¶ Changes the export format to a comma separated values (CSV) format. By default
mongoexportwrites data using one JSON document for every MongoDB document.If you specify
--csv, then you must also use either the--fieldsor the--fieldFileoption to declare the fields to export from the collection.
-
--jsonArray¶ Modifies the output of
mongoexportto write the entire contents of the export as a single JSON array. By defaultmongoexportwrites data using one JSON document for every MongoDB document.
-
--slaveOk,-k¶ Allows
mongoexportto read data from secondary or slave nodes when usingmongoexportwith a replica set. This option is only available if connected to amongodormongosand is not available when used with the “mongoexport --dbpath” option.This is the default behavior.
-
--out<file>,-o<file>¶ Specify a file to write the export to. If you do not specify a file name, the
mongoexportwrites data to standard output (e.g.stdout).
-
--forceTableScan¶ New in version 2.2.
Forces
mongoexportto scan the data store directly instead of traversing the_idfield index. Use--forceTableScanto skip the index. 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,mongoexportmay return a document more than once if a write operation interleaves with the operation to cause the document to move.Warning
Use
--forceTableScanwith extreme caution and consideration.- If you have key sizes over 800 bytes that would not be present
in the
Usage¶
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.
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.
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.
Warning
The above example will only succeed if there is no mongod
connected to the data files located in the /srv/mongodb/
directory.
The final 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.