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:
Go to the directory where MongoDB is kept.
Go to its Bin folder.
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.
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)
Type the undermentioned command:
mongoimport -d wordCupDictionary -c dailywords --type CSV --file word.csv --headerline
And you will see that your document data will be imported.
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.
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.