Updated June 19, 2023
Introduction to MongoDB Import
The MongoDB Import command, known as mongoimport, is actively used to import documents from various file formats such as TSV, CSV, or JSON into MongoDB. The command line tool used for this purpose is called “mongoimport.” MongoDB import is 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 loads the data into the collection using the CSV, TSV, and json files.
- Uri: This is referred to as the “connection URL” that is used when importing data from a file into MongoDB.
- User name: This is defined as the username which we have used while importing data from the file.
- User password: This refers to the “user’s password” that is used during the process of importing data from a file in MongoDB.
- Host name: This is defined as the hostname of the database server we have used while importing data from the file.
- Port no: This is known as the “port number” of the database server, which is specified when importing data from a file into MongoDB.
- Database name: This refers to the “name of the database” from which the collection’s data is imported from the file.
- Collection name: This is the collection name from which we are importing data from the file in MongoDB.
- Authentication database: We must define an authentication database while importing data from the file.
- File name: This parameter is defined as the name of file from which we are importing data into the collection. We can import the data from CSV, TSV, and JSON files in MongoDB.
- Replica set name: While importing data from file, we have to define the replica set name while importing data into the collection.
How Import Command works in MongoDB?
We need to define the database name, username, port number, password of the user, and import file name at the time of importing data from the file into the collection. We have used extended json format data to import data into the collection from json files. In the newer version of MongoDB, we have used an extended json format data file to import documents into the collection from the file.
We have used the following options with mongoimport command in MongoDB when 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
Starting from MongoDB version 4.2, the insertion of documents from a JSON file follows the order in which they appear in the file. The bulk order insertion data is possible by using the mongoimport command in MongoDB. We have maintained an 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 a JSON file.
The below example shows importing the data into the collection by using a JSON file. We have imported the data into the import collection. We have used the database name as admin at the time of insertion of 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 a CSV file.
The below example shows importing the data into the collection by using a CSV file. We have imported the data into the import1 collection. We have used the database name as admin at the time of insertion of 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 the TSV file.
The below example shows that importing the data into the collection by using a TSV file. We have imported the data into the import2 collection. We have used the database name as admin at the time of insertion of 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 and how the import command works in MongoDB. and examples, respectively. You may also have a look at the following articles to learn more –