- Reference >
- MongoDB Package Components >
mongoimport
mongoimport¶
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
-vform by including the option multiple times, (e.g.-vvvvv.)
-
--version¶ Returns the version of the
mongoimportprogram.
-
--host<hostname><:port>,-h¶ Specifies a resolvable hostname for the
mongodto which you want to restore the database. By defaultmongoimportwill attempt to connect to a MongoDB process running on the localhost port numbered27017.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
--hostargument with a setname, followed by a slash and a comma-separated list of host and port names.mongoimportwill, given the seed of at least one connected set member, connect to the primary 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.
-
--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 themongoimport --hostcommand.
-
--ipv6¶ Enables IPv6 support that allows
mongoimportto connect to the MongoDB instance using an IPv6 network. All MongoDB programs and processes, includingmongoimport, disable IPv6 support by default.
-
--ssl¶ New in version 2.4: MongoDB added support for SSL connections to
mongodinstances 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
mongodinstances that require client certificate validation.Allows
mongoimportto 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
mongoimport --passwordoption to supply a password.
-
--password<password>,-p<password>¶ Specifies a password to authenticate to the MongoDB instance. Use in conjunction with the
mongoimport --usernameoption to supply a username.If you specify a
--usernameand do not pass an argument to--password,mongoimportwill prompt for a password interactively. If you do not specify a password on the command line,--passwordmust 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,
mongoimportassumes 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,mongoimportalso 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 enablesmongoimportto attach directly to local data files and insert the data without themongod. To run with--dbpath,mongoimportneeds 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 allowsmongoimportto 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--dbpathoption.
-
--journal¶ Allows
mongoimportwrite 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--dbpathoption.
-
--db<db>,-d<db>¶ Use the
--dboption to specify a database formongoimportto import data.
-
--collection<collection>,-c<collection>¶ Use the
--collectionoption to specify a collection formongoimportto 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
--fieldsthe--fieldFileoption 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,
mongoimportcreates 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.
mongoimportwill 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,mongoimportwill 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
--upsertFieldsmongoimportwill upsert on the basis of the_idfield.
-
--upsertFields<field1[,field2]>¶ Specifies a list of fields for the query portion of the upsert. Use this option if the
_idfields 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
mongoimportto 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 --jsonArrayto 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:
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.
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.
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.