Navigation
This version of the manual is no longer supported.

mongoimport

On this page

Synopsis

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.

Note

Do not use mongoimport and mongoexport for full instance, production backups because they will not reliably capture data type information. Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.

Options

mongoimport
--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 -v form by including the option multiple times, (e.g. -vvvvv.)

--version

Returns the version of the mongoimport program.

--host <hostname><:port>, -h

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 running 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 the primary of that set. This option would resemble:

--host repl0/mongo0.example.net,mongo0.example.net:27018,mongo1.example.net,mongo2.example.net

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

--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 the mongoimport --host command.

--ipv6

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.

--ssl

New in version 2.4: MongoDB added support for SSL connections to mongod instances in mongoimport.

Note

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.

--username <username>, -u <username>

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.

--password <password>, -p <password>

Specifies a password to authenticate to the MongoDB instance. Use in conjunction with the mongoimport --username option to supply a username.

If you specify a --username and do not pass an argument to --password, mongoimport will prompt for a password interactively. If you do not specify a password on the command line, --password must be the last option.

--authenticationDatabase <dbname>

New in version 2.4.

Specifies the database that holds the user’s (e.g --username) credentials.

By default, mongoimport assumes that the database specified to the --db argument 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, mongoimport also includes support for GSSAPI to 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 --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.

--directoryperdb

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.

--journal

Allows mongoimport write to the durability journal to ensure that the data files will remain in a valid state during the write process. This option is only relevant when specifying the --dbpath option.

--db <db>, -d <db>

Use the --db option to specify a database for mongoimport to import data.

--collection <collection>, -c <collection>

Use the --collection option to specify a collection for mongoimport to import.

--fields <field1<,field2>>, -f <field1[,field2]>

Specify a comma separated list of field names when importing csv or tsv files that do not have field names in the first (i.e. header) line of the file.

--fieldFile <filename>

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.

--ignoreBlanks

In csv and tsv exports, ignore empty fields. If not specified, mongoimport creates fields without values in imported documents.

--type <json|csv|tsv>

Declare the type of export format to import. The default format is JSON, but it’s possible to import csv and tsv files.

--file <filename>

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.

--drop

Modifies the import process so that the target instance drops every collection before importing the collection from the input.

--headerline

If using “--type csv” or “--type tsv,” use the first line as field names. Otherwise, mongoimport will import the first line as a distinct document.

--upsert

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.

--upsertFields <field1[,field2]>

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.

--stopOnError

New in version 2.2.

Forces mongoimport to halt the import operation at the first error rather than continuing the operation despite errors.

--jsonArray

Changed in version 2.2: The limit on document size increased from 4MB to 16MB.

Accept import of data expressed with multiple MongoDB documents within a single JSON array.

Use in conjunction with mongoexport --jsonArray to import data written as a single JSON array. Limited to imports of 16 MB or smaller.

Usage

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 determines the name of files using the first line in the CSV file, because of the --headerline:

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

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
←   mongooplog mongoexport  →