Importing CSV files in MongoDB is not as tough a task as it seems to be. With the advent of technological features in NoSQL, MongoDB also provides the features to import or export the files. MongoDB is one of the best NoSQL databases that has evolved in recent years. It provides the feature-full services to give the best results as expected by any developer or database expert.
Have you ever tried importing a CSV file in MongoDB? if not and you want it in your project to be done then you are in the right place. In this blog, I am going to tell you how to do so step by step.
But before that, let’s just check out what are CSV files?
What are CSV files?
CSV is the acronym for the comma-separated values file which allows saving the data in the table structured format. CSVs generally look like garden-variety spreadsheet data but they have an extension .csv. Generally, we save them in the text file format which is comma-separated-values. The same data which we see in CSV can be seen in a spreadsheet with rows and columns.
We can use CSV files with any spreadsheet program such as Microsoft Excel, Google spreadsheet, etc.
Importance of importing a CSV file
Import, see the word itself explains the importance. CSVs are the plain textual form of content that is kept inside a file. Let us just focus on why CSVs are important:
- Opened inside any text editor, unlike other file formats.
- Fairly flat and simple schema
- Programming languages can easily parse their data and generating them is easy.
So, importing CSV files in MongoDB also helps the developer to easily import the data by just reading the content inside it. Let’s just go through the steps to import a CSV file.
Importing a CSV file in MongoDB – steps and commands
Importing a CSV file in MongoDB is done through some commands which are followed step by step.
Follow these simple steps to import the CSV:
Step 1
Go to the directory where MongoDB is kept.
Step 2
Go to its Bin folder.
Step 3
Save or keep your CSV or TXT file here which you want to import. The image below shows exactly what you need to do.

This is our CSV file named word.csv.
The top word is the headerline which will be used in the command line MongoDB import command later.
Step 4
Now into the same folder open the cmd by pressing ctrl+L and typing cmd there. Press enter and you will see a cmd window now. (You can open this cmd window at this place by right-clicking the mouse along with shift key pressed – here you will see open command window here)
Step 5
Type the undermentioned command:
1 |
mongoimport -d wordCupDictionary -c dailywords --type CSV --file word.csv --headerline |

And you will see that your document data will be imported.

IMPORTANT NOTE:
wordCupDictionary is the database name
dailywords is the collection name.
–type CSV denotes that file type that is being imported is of type CSV.
word.csv is the file name that we are importing.
–headerline is the header of the CSV file, below which listed data is mentioned (here in this example it is ‘word ‘).
Following these steps, you will be successfully importing the CSV file inside the MongoDB database. This was the simple process inside MongoDB to achieve the target of MongoDB CSV file import.
MongoDB offers many other features that you may be interested in. For that you may like to read our blog.
Why choose MongoDB over other Databases?
Bottom Line:
In this blog cum tutorial, I have tried to give you a very brief step by step solution for importing a CSV file in MongoDB. In the same blog, I have tried explaining you the commands with the example.
Habilelabs is a one-stop solution for all mobile and web application development using MEAN, MERN, and full-stack technology. It is the official MongoDB ready partner and provides the development and consulting on MongoDB based solutions. Give us a call to find out more about our awesome services!
I hope this blog must have given you what you were looking for. In case you find yourself in some technical problem or have a query, then do let us know in the comments section below. Also, share your views about the blog that would really help me in coming up with some more knowledgeable content.
Hi,
While doing mongoimport –type csv we can provide –fieldfile as an option which has fields of json structure like emp.name, emp.0.place, address.1.city, etc..
we can specify all this in one file one line per field.
but when we do import the data shown is datatype mismatch number is replaced in strings.
so how can we specify datatypes for the fields seperated with dot either in –fieldfile or –field option.
Hello Sugumar,
Mongoimport takes by default datatype based on your data but we can specify data type with the help of “–columnsHaveTypes”
For an example :
mongoimport -d wordCupDictionary -c dailywords –type CSV –fields col1.int32\(\),col2.double\(\),col3.string\(\) –columnsHaveTypes –file path/to/file.txt
Regards
Habilelabs team
Thank u so much Ashu.
Hii I am facing problem for to import csv file into mongodb database
this error i am facing error validating settings: incompatible options: –file and positional argument(s)
and this is the code
mongoimport -d screenCaller -c screenCaller –type csv -file C:/xampp/htdocs/new lata-npa-nxx-state-city-zip.csv -headerline
hello Michal,
Seems like your systax is incorrect, -file should be –file(double hyphen without space) and same for –headerline(double hyphen without space)
Correct syntax for mongoimport is following :
mongoimport -d screenCaller -c screenCaller – -type csv – -file C:/xampp/htdocs/new lata-npa-nxx-state-city-zip.csv – -headerline
Hope it will help.
Regards
Habilelabs Team
\bin>mongoimport -d db1 -c sales –type csv –file C:\Program Files\MongoDB\sales.csv –headerline
2018-04-19T16:20:58.574+0530 error validating settings: incompatible options: –file and positional argument(s)
2018-04-19T16:20:58.574+0530 try ‘mongoimport –help’ for more information
Hi Prakriti,
seems like your syntax is incorrect. someone asked same question so please look at above comment.
if you still face any issue let us know.
Thanks