The MongoDB database features a built-in functionality called mongoimport to import files like JSON, CSV, and TSV. It is a CLI utility, often used to bring in data from other MongoDB databases or third-party export tools. Oftentimes, application development within teams requires the exchange of files. And this is where mongoimport comes in handy.

The mongoimport command plays a significant role in managing the MongoDB database. The mongoimport json command will create the database and the collection if they do not exist. In this tutorial, we’ll show you how to import all three file types to your MongoDB database instance in a few short steps. Read on to find a detailed guide.

MongoDB mongoimport Command Considerations

As a precautionary note, avoid using mongoimport and mongoexport for entire instance production backups. This is because mongoimport and mongoexport commands can not consistently retain all rich BSON data types, and JSON only represents a subset of the kinds provided by BSON. For this type of backup functionality, we recommend you to instead use mongodump and mongorestore.

To clarify how mongoexport and mongoimport command uses strict mode representation to retain information, here’s an example of an insert operation in the mongo shell that uses the shell mode representation for the BSON types “data_date” and “data_numberlong”:

use test
db.traffic.insert( { _id: 1, volume: NumberLong('2980000'), date: new Date() } )

The argument passed to data_numberlong has to be quoted to avoid potential loss of accuracy. Now, when we use mongoexport to export the data:

mongoexport --db test --collection traffic --out traffic.json

The exported data pops out in a strict mode representation to preserve type information:

{ "_id" : 1, "volume" : { "$numberLong" : "2980000" }, "date" : { "$date" : "2014-03-13T13:47:42.483-0400" } }

How to Import Data into MongoDB Using mongoimport

As mentioned previously, the mongoimport command is used to import JSON, CSV, or TSV files. For a clear content structure and easy navigation, we’ve listed steps to import data from JSON, CSV, or TSV files into three different parts. 

Simplify MongoDB ETL with Hevo’s No-Code Data Pipeline

Hevo is the only real-time ELT No-code Data Pipeline platform that cost-effectively automates data pipelines that are flexible to your needs. With integration with 150+ Data Sources (40+ free sources), we help you not only export data from sources & load data to the destinations but also transform & enrich your data, & make it analysis-ready.

Start for free now!

Get Started with Hevo for Free

Before proceeding to these sections, you must first ensure to connect mongoimport Windows, macOS, or Ubuntu utility to your MongoDB instance. While there are several ways to connect mongoimport to your MongoDB database, we recommend you to use the –uri option, like this:

mongoimport --uri 'mongodb+srv://' 

Note: If you are using MongoDB Atlas Deployment, you can find your Atlas connection string when you visit Database Deployments > Connect

If you aren’t using Atlas Deployment, you must generate your own URI. If you are connecting to a single server, your URL will look like this – mongodb://, and if you are running a replicaset and connecting to multiple hostnames, your URL will look like this- mongodb://username:password@host1:port,host2:port/?replicaSet=replicasetname.

For more information, check out the documentation page here

Part 1: Importing JSON Files

To import JSON file(s) from a collection, refer to the provided mongoimport example:

mongoimport --db DB_Name --collection Collection_Name --type=json --
file Name-of-file-to-import


  • DB_Name represents the name of the database that contains the collection Collection_Name.
  • type specifies the file type JSON (Optional field).
  • Name-of-file-to-import represents the name and path of the JSON file to be imported/restored.

In a concise way, you can also write the above code as:

mongoimport -d DB_NAME -c COLLECTION_name --file Name-of-file-to-import

In case, you would like to drop any existing collection with the same name as the one you’re trying to create/import, you can inform the same to mongoimport command using the –drop flag.

mongoimport -d DB_NAME -c COLLECTION_name --drop --file Name-of-file-to-import

If you want to change your host or port number while importing JSON data, you can do so using the following MongoDB mongoimport command. 

mongoimport --host --port 1234 -d DB_NAME -c COLLECTION_name --file Name-of-file-to-import

By default, mongoimport connects to a running mongo on port 27017 on localhost

Part 2: Importing CSV Files

The mongoimport command is used for performing a MongoDB import CSV operation. You can use the mongoimport command to import CSV files into a collection using the headerline option. Headerline option notifies the mongoimport command of the first line; to be not imported as a document since it contains field names instead of data.

To import a collection from a CSV file, use the code as mentioned below:

mongoimport --db DB_Name --collection Collection_Name --type=csv --
headerline --file=Name-of-file-to-import


  • DB_Name represents the name of the database that contains the collection Collection_Name.
  • type specifies the file type CSV (Optional field).
  • headerline details the mongoimport command to take the 1st record of CSV file(s) as field names. 
  • Name-of-file-to-import represents the name and path of the CSV file to be imported/restored.

Importing CSV Files Lacking Header

In the event your CSV file doesn’t have a header row, then you must notify the mongoimport command of the names of each column included in your file. There are two ways of doing so:

  • Specify Field Types
  • Use a Field File

Specify Field Types

One way is to write field names on the command-line using the –fields option. This can prove cumbersome if your CSV file contains lots of columns, as can be seen from the command below:

   --fields="username,”"identifier","one time password","recovery password","recovery account","one time code","recovery code","user id","first name","last name","birth year","gender",”department”,”company”,”country”

Use a Field File

The other method is to notify the mongoimport command on field columns using a separate .txt file. Here we’ve created a file called “Field_file.txt” containing all column names present in our CSV file.

one time password
recovery password
recovery account
one time code
recovery code
user id
first name
last name
birth year

After producing such a file, you can run the following command which will use your .txt file (in our example “Field_file.txt”) to extract information on these column names as field names in MongoDB:

   --collection= Collection_Name

Part 3: Importing TSV Files

TSV files are conceptually the same when compared with CSV file formats. Whether you use mongoimport Windows utility or another one, as a result, you may import TSV files using the same technique as described for CSV files. 

There’s only one minor change to keep in mind- instead of using the –type=csv, you can change it and use the –type=tsv option to tell mongoimport of the new format.

mongoimport Command Options

The mongoimport command can be appended with options that help developers explore and achieve more. While an extensive list of supported options can be found on the MongoDB mongoimport documentation page, in this blog, we’ve featured only the important ones:

–helpReturns information on the options and use of mongoimport.
–host=<hostname><:port>, -h=<hostname><:port>Used to specify the hostname to which mongoimport should connect. By default, this is set to localhost:27017.
–ignoreBlanksIgnore fields or columns with empty values.
–dropModifies the import process so that the target instance drops the collection before importing the data from the input.
–stopOnErrorForces mongoimport to halt insert operation at the first error instead of continuing.
–mode=<insert|upsert|merge|delete>Tells how mongoimport should handle existing documents in the database that match documents in the import file.

And this brings us to the end of mongoimport Windows, macOS, Ubuntu command utility. 

Take a peek at a few more interesting blogs to pique your interest:


We hope this blog piece sparked your interest and served you with in-depth information on one of the powerful MongoDB Package Component tools that is provided natively- the mongoimport command. Using this guide, we know that you can successfully use the mongoimport command for your own use of web applications and use-cases, effortlessly.

If your business actively uses MongoDB for developing and hosting your web applications, there’s a high chance you would want to have a secure backup for your MongoDB database. While exporting your MongoDB databases manually can get unwieldy and not so fun, a better and faster method is to use a Cloud-based Data Warehouse for your data storage and transformation needs. A simple and speedy solution like Hevo Data helps you with that. 

Visit our Website to Explore Hevo

Why not give Hevo a try? Sign Up here for a 14-day free trial and experience the feature-rich Hevo suite first hand. You can also check our Hevo pricing and make a decision on your best-suited plan. 

Share your opinions about learning the procedure for using mongoimport for MongoDB in the comments area below. We’d like to hear your thoughts and ideas.

Divyansh Sharma
Marketing Research Analyst, Hevo Data

Divyansh is a Marketing Research Analyst at Hevo who specializes in data analysis. He is a BITS Pilani Alumnus and has collaborated with thought leaders in the data industry to write articles on diverse data-related topics, such as data integration and infrastructure. The contributions he makes through his content are instrumental in advancing the data industry.

Sarthak Bhardwaj
Customer Experience Engineer, Hevo

Sarthak brings two years of expertise in JDBC, MongoDB, REST API, and AWS, playing a pivotal role in Hevo's triumph through adept problem-solving and superior issue management.

No-code Data Pipeline for MongoDB