Introduction to MongoDB Import
MongoDB Import command is used to import the document from the TSV, CSV or JSON file in MongoDB, mongoimport is the command line tool used for import the data from the file. MongoDB import is the part of the tool package in MongoDB, we can download this import package from the official download center.
Syntax of MongoDB Import
Below is the syntax of MongoDB Import.
Mongoimport --host (host name) -u (name of user) -p (password of user) --authenticationDatabase (admin database used for authentication)Â Â --db (Name of database) --collection (name of collection) --drop --file /name_of_file (Name of file which was we have used to import into collection)
Below is the parameter description syntax of MongoDB Import:
- Mongoimport: This command is used to load the data into collection by using the CSV, TSV and json files.
- Uri: This is defined as connection URL which we have used while importing data from file in MongoDB.
- User name: This is defined as username which we have used while importing data from the file.
- User password: This is defined as password of user which we have used while importing data from the file.
- Host name: This is defined as host name of database server which we have used while importing data from the file.
- Port no: This is defined as port no. of database server which we have used while importing data from the file.
- Database name: This is defined as name of database from which collection we are importing data from the file.
- Collection name: This is defined as name of collection from which we are importing data from the file in MongoDB.
- Authentication database: We need to define authentication database while importing data from the file.
- File name: This parameter is defined as name of file from which we are importing data into the collection. We can import the data from CSV, TSV and JSON file in MongoDB.
- Replica set name: While importing data from file we have to define replica set name while importing data into the collection.
How Import Command works in MongoDB?
We need to define database name, username, port number, password of user, and import file name at the time of importing data from file into the collection. We have used extended json format data to import data into the collection from json file. In newer version of MongoDB we have used extended json format data file to import document into the collection from the file.
We have used following options with mongoimport command in MongoDB at the time of importing data into the collection.
- Database name
- Collection name
- User name
- User password
- Host name
- Port name
- Authentication database
- Replica set name
- Authentication mechanism
- File name
- Drop keyword
From MongoDB version 4.2 we have insert the document from the json file will be inserted in order which they will appeared into the file. Document will be sequentially insert into the collection as they will appeared from the input file. The bulk order insertion data is possible by using the mongoimport command in MongoDB. We have maintained insertion order flag to insert the documents as per the insertion order in MongoDB.
Examples
Given below are the examples of MongoDB Import:
Example #1
Import the data into the collection by using JSON file.
Below example shows that import the data into the collection by using JSON file. We have imported the data into import collection. We have used database name as admin at the time of insertion data into the import collection.
Code:
mongoimport --db admin --collection import --type json --file test.json
mongo
use admin
db.import.find()
Output:
Example #2
Import the data into the collection by using CSV file.
Below example shows that import the data into the collection by using CSV file. We have imported the data into import1 collection. We have used database name as admin at the time of insertion data into the import1 collection.
Code:
mongoimport --db admin --collection import1 --file test.csv
mongo
use admin
db.import1.find()
Output:
Example #3
Import the data into the collection by using TSV file.
Below example shows that import the data into the collection by using TSV file. We have imported the data into import2 collection. We have used database name as admin at the time of insertion data into the import2 collection.
Code:
mongoimport --db admin --collection import2 --file test.tsv
mongo
use admin
db.import2.find()
Output:
Recommended Articles
This is a guide to MongoDB Import. Here we discuss the introduction, how import command works in MongoDB? and examples respectively. You may also have a look at the following articles to learn more –
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
View Course
Related Courses