The mongoimport tool provides a route to import content from a JSON, CSV, or TSV export created by mongoexport, or potentially, another third-party export tool. See the “Import and Export MongoDB Data” document for a more in depth usage overview, and the “mongoexport” document for more information regarding mongoexport, which provides the inverse “importing” capability.
Returns a basic help and usage text.
Increases the amount of internal reporting returned on the command line. Increase the verbosity with the -v form by including the option multiple times, (e.g. -vvvvv.)
Returns the version of the mongoimport program.
Specifies a resolvable hostname for the mongod to which you want to restore the database. By default mongoimport will attempt to connect to a MongoDB process ruining on the localhost port numbered 27017.
Optionally, specify a port number to connect a MongoDB instance running on a port other than 27017.
To connect to a replica set, use the --host argument with a setname, followed by a slash and a comma-separated list of host and port names. mongoimport will, given the seed of at least one connected set member, connect to primary node of that set. This option would resemble:
You can always connect directly to a single MongoDB instance by specifying the host and port number directly.
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 the mongoimport --host command.
Enables IPv6 support that allows mongoimport to connect to the MongoDB instance using an IPv6 network. All MongoDB programs and processes, including mongoimport, disable IPv6 support by default.
New in version 2.4: MongoDB added support for SSL connections to mongod instances in mongoimport.
SSL support in mongoimport is not compiled into the default distribution of MongoDB. See Connect to MongoDB with SSL for more information on SSL and MongoDB.
Additionally, mongoimport does not support connections to mongod instances that require client certificate validation.
Allows mongoimport to connect to mongod instance over an SSL connection.
Specifies a username to authenticate to the MongoDB instance, if your database requires authentication. Use in conjunction with the mongoimport --password option to supply a password.
Specifies a password to authenticate to the MongoDB instance. Use in conjunction with the mongoimport --username option to supply a username.
New in version 2.4.
Specifies the database that holds the user’s (e.g --username) credentials.
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, mongoimport also includes support for GSSAPI to handle Kerberos authentication.
See Deploy MongoDB with Kerberos Authentication for more information about Kerberos authentication.
Specifies the directory of the MongoDB data files. If used, the --dbpath option enables mongoimport to attach directly to local data files and insert the data without the mongod. To run with --dbpath, mongoimport needs to lock access to the data directory: as a result, no mongod can access the same path while the process runs.
Use the --directoryperdb in conjunction with the corresponding option to mongod, which allows mongoimport to import data into 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.
Allows mongoexport write to the durability journal to ensure that the data files will remain in a consistent state during the write process. This option is only relevant when specifying the --dbpath option.
Use the --collection option to specify a collection for mongoimport to import.
As an alternative to --fields the --fieldFile option allows you to specify a file (e.g. <file>) to that holds a list of field names if your csv or tsv file does not include field names in the first (i.e. header) line of the file. Place one field per line.
Specify the location of a file containing the data to import. mongoimport will read data from standard input (e.g. “stdin.”) if you do not specify a file.
Modifies the importation procedure so that the target instance drops every collection before restoring the collection from the dumped backup.
Modifies the import process to update existing objects in the database if they match an imported object, while inserting all other objects.
If you do not specify a field or fields using the --upsertFields mongoimport will upsert on the basis of the _id field.
Specifies a list of fields for the query portion of the upsert. Use this option if the _id fields in the existing documents don’t match the field in the document, but another field or field combination can uniquely identify documents as a basis for performing upsert operations.
To ensure adequate performance, indexes should exist for this field or fields.
New in version 2.2.
Forces mongoimport to halt the import operation at the first error rather than continuing the operation despite errors.
Changed in version 2.2: The limit on document size increased from 4MB to 16MB.
Accept import of data expressed with multiple MongoDB document within a single JSON array.
In this example, mongoimport imports the csv formatted data in the /opt/backups/contacts.csv into the collection contacts in the users database on the MongoDB instance running on the localhost port numbered 27017.
mongoimport --db users --collection contacts --type csv --file /opt/backups/contacts.csv
In the following example, mongoimport imports the data in the JSON formatted file contacts.json into the collection contacts on the MongoDB instance running on the localhost port number 27017. Journaling is explicitly enabled.
mongoimport --collection contacts --file contacts.json --journal
In the next example, mongoimport takes data passed to it on standard input (i.e. with a | pipe.) and imports it into the collection contacts in the sales database is the MongoDB datafiles located at /srv/mongodb/. if the import process encounters an error, the mongoimport will halt because of the --stopOnError option.
mongoimport --db sales --collection contacts --stopOnError --dbpath /srv/mongodb/
In the final example, mongoimport imports data from the file /opt/backups/mdb1-examplenet.json into the collection contacts within the database marketing on a remote MongoDB database. This mongoimport accesses the mongod instance running on the host mongodb1.example.net over port 37017, which requires the username user and the password pass.
mongoimport --host mongodb1.example.net --port 37017 --username user --password pass --collection contacts --db marketing --file /opt/backups/mdb1-examplenet.json