MongoDB - How to Import and Export CSV Data in MongoDB
In this post we take a look at How to Import and Export CSV Data in MongoDB. Previously we looked at the Import and Export JSON data in MondoDB. For this tutorial too we are making use of the MongoDB Compass. And if you an do this things would be lot easier to work with.
There are two methods to use this tutorial. One you can do with the mongodb compass and another being the command line. Below I am giving link to video and also the explained command line method.
So what do you need to get started with the MongoDB for importing the CSV data? Make sure to have the MongoDB installed. Second thing you have to consider installing the MongoDB compass. And once we have that setup and installed you can run the mongod.exe or through the command line for further usage.
If you are short on time, go through video tutorial below as it would save you time and you would get visual idea too.
First thing I assume you have done is installed the MongoDB database. Second thing you have to think is making sure to install the mongodb compass. In windows you can use the mongodb compass icon to fire it up. In case of linux make sure to open the command line and then type in the command : mongodb-compass. This would open up the mongodb compass application.
So here are the steps to import the CSV file into the mongodb database using Compass.
- Connect to the MongoDB database through connection.
- Provide the connection string, parameters required for the connection.
- Create a database. Give that database a name.
- Create a collection, give it a name too.
- Make sure to have the CSV data to import into the database.
- Click on the Add Data button. Choose CSV file.
- Import the CSV sample data into the database.
- Upload the file and wait for the confirmation of data upload
- Click import
This should import the CSV data into the database. And once uploaded you would be able to fire up the query and view the content of that database. It would appear in a json format though but still you can see the tabular view as well. Now let's take a look at the command line version for the CSV upload.
Mongoimport for CSV
This is another method where we are making use of the command line tool which is available on both windows and linux. And you can use it to upload the data to the database. And the CSV data can be then used for the importing. So here's the code that you can use for the import.
- Database: DemoDb
- Collection: demo
- Data: sample.csv
Here's the code to import it.
mongoimport --type csv --headerline --db DemoDb --collection demo --file C:\Users\Documents\sample.csv
That's it. When you import the csv file you would the tabular structure taken for the json data and the collection is filled with document for each line. And that would appear. You can even verify with the mongodb compass. This would be perfect for you to check out with gui tool.
I have made 3 videos and I have covered all of them here. Now next week I will cover few other videos and hopefully you would love checking some of those database tutorials too. It's a good option for anyone who wants to check out the database options there. You can check my channel from the above video and from the playlist you can even get more such database specific videos
If you happen to like this content, do give me feedback over there and that would help me improve my efforts in near future.
Congratulations @devpress! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)
Your next target is to reach 600 posts.
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
To support your work, I also upvoted your post!
Congrats!
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.