We need import a large or any size of data set to practice or for the project requirement. We can insert a large data set using insertMany()
but it is not too handy. Mongo Db provide us a better approach to import data using mongoimport
tool.
The
mongoimport
tool imports content from an Extended JSON, CSV, or TSV export created bymongoexport
, or potentially, another third-party export tool.
Importing JSON Data:
mongoimport --db <my_db_name> --collection <my_collection_name> --file <my_file_name.json>
--db
: specifies the name of the database on which to runmonogimport
. If that database does not exist, it will be created.--collection
: specifies the name of the collection.--file
: specifies the location and name of the file to be imported. If you have already navigated to the directory containing the data file, simply typing the file name will suffice, as in this case.
Importing a JSON Array:
mongoimport --db <my_db_name> --collection <my_collection_name> --file <my_file_name.json> --jsonArray
The syntax for importing JSON arrays is similar to importing JSON objects with the addition of one option:
--jsonArray
: specifies that the file is a JSON array.
Importing CSV / TSV files:
mongoimport --db <my_db_name> --collection <my_collection_name> --file <my_file_name.csv> --type csv --headerline
In addition to the options you’ve already come across, the query above contains:
--type
: specifies the file type to import. Can becsv
ortsv
. The default value is JSON hence we didn’t use this flag when importing JSON data earlier.--headerline
: indicates that the first line of the file is the header line. If this flag is not used,mongoimport
will import the first line of the file as a separate document. This flag only works withcsv
andtsv
file types. If used with JSON, an errror will be thrown.--fields
: specifiy a comma separated list of field names, in cases where the data file being imported does not have a header line in the first line. This flag only works withcsv
andtsv
file types. If used with JSON, an error will be thrown. We didn’t need to use this flag in our query since ourcsv
files have a header line as the first line.
There are many other parameter that we can pass with this command for more details check here.
For any issue with mongoimport check out this post mongoimport issues.
Reference : Mongo Db Practice