EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Import
 

MongoDB Import

Priya Pedamkar
Article byPriya Pedamkar

Updated June 19, 2023

MongoDB Import

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

MongoDB Import 1

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:

MongoDB Import 2

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:

collection by using TSV file

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 –

  1. MongoDB Skip()
  2. MongoDB Auto Increment
  3. MongoDB Sharding
  4. MongoDB Features

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW