Expert Group of programming
Loading...

How to Import a CSV/json/tsv into MongoDB

Posted by : Heycode Tech | Posted in : MongoDB | Mar 23,2016 | 0 comments

As we know that MONGODB is one of the most popular database.Here we are going to describe about the MONGO IMPORT i.e importing a CSV/JSON/TSV file into the MONGODB.

I have tested this command on LINUX-UBUNTU System.

I understand that you have already created the DATABASE as well as COLLECTION in MONGODB.

Here we are describing the two methods for importing the DATA .
1: Import the complete Database.
2: Import the single collection data.

1: Import the complete Database

in mongodb 3.0 or above, we should specify the database name to restore. Below is the command which is used to restore .

mongorestore -d [your_db_name] [your_dump_dir]

You need to put the value of database name and dump directory.
Suppose your database Name = ‘heycodetech’ and dump directory path = /home/heycodetech/Document/dumpDB
then command is used –

mongorestore -d heycodetech /home/heycodetech/Document/dumpDB

For exporting the dump, Click Here for more in details .

2: Import the single collection data

For importing the single collection,we need to follow this syntax.


mongoimport ‐‐host=127.0.0.1 -d dbname -c collectionName ‐‐type csv/json/tsv ‐‐file filename.csv ‐‐headerline

Now you need to change the following details .
dbname = Database Name
collectionName= Collection of mongoDB
csv = Define the fileType like csv,tsv or json
filename.csv = Please Enter the correct path of the file .
Note :
1: Filename must be match with filetype otherwise it creates problem.
2: Please don’t forgot “–headerline” in the last.

Let’s take an example.
Here is the following details like DBNAME=heycodetech and collection Name=secure and fileName=data.csv


Command used :
mongoimport ‐‐host=127.0.0.1 -d heycodetech -c secure ‐‐type csv ‐‐file data.csv ‐‐headerline

mongoImport

Import the json file into MongoDB
We have taken all the information as same except the file name i.e data.json. So here is the correct syntax.


Command used :
mongoimport ‐‐host=127.0.0.1 -d heycodetech -c secure ‐‐type json ‐‐file data.json ‐‐headerline

Please review it and let me know if you have any issues/suggestion.Drop one comment and our expert will respond quickly.

Thanking you .

Tags:

Author information - Heycode Tech

Every world can ask question and my website registered user give the suggestion.

See all posts by Author

Connect to author

About us

Heycodetech is a platform where you can ask any questions related to the programming like PHP,jQuery, MYSQL,HTML , CSS and moreHere is the Expert group of programming.