In this post we will learn How to use mongoimport to import CSV files for the same, we simply need to follow certain steps and execute some commands and the task is done same we have given the steps along with the required command below.
Import CSV File
As we know at times we need certain CSV files to import into our MongoDB database to add and for the same purpose we need to add another CSV file from an external source to do the same we have to follow a set of steps to get it done which we have given below and we can do so by simply follow the steps given below.
Prepare a CSV File
As to importing a csv file, we need a csv file that could be already present in your computer but in this case, we are starting from the beginning so we are creating the file by ourselves so first we can create it as per our choice.
We need to open the now command prompt in case you are using a window and in the case of Linux or mac, we need to open the terminal.
Navigate to Directory
Now we need to direct our command to the directory where we actually stored the csv file. by simply giving the address with the help of cd.
We can do the same thing by simply opening the csv file location and in the address bar of file explorer type cdm and the command prompt of the same directory will be one by this process we can simply skip the second step os opening the command prompt and navigating to the address of csv file stored.
Now in the command prompt, we simply need to type mongoimport and the command given below to execute there and the file will be imported.
mongoimport --type csv --headerline --db yourdb --collection yourcollection --file yourfile.csv
- Type CSV specifies that the input file is a CSV file.
- header line specifies that the first line of the input file contains headers.
- db your db specifies the name of the database to import into.
- Collection Your collection specifies the name of the collection to import into.
- file your file.csv specifies the name of the input file
Now the file has been imported and we can also verify it
Verify The File
When we run the command mongoimport we would have seen a message indicating as a number of documents are imported which nothing but shows that we had imported successfully.
To learn more about How to use mongoimport to import CSV files visit: by MongoDB
To learn more about MongoDB and tutorials related to it visit: MongoDB Problems And Tutorials