Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to import csv file in mongodb

Tags:

mongodb

I have a csv file containing following data and want to import it in mongodb

ID;"AdmissionID";"SeatNo";"RegistrationNo";"ResultDate";"ResultStatusId"
1;12;"2323";"23";07-05-2013;1
2;23;"35";"32";10-05-2013;5

this data is to be imported to mongodb 2.2. I'm using following command:

mongoimport -d test -c exam --type csv --headerline <f:\exam.csv

when used i get following error

SyntaxError: missing ; before statement (shell):1

please help me to find out the error

like image 922
user2357929 Avatar asked May 07 '13 10:05

user2357929


People also ask

How do I import a CSV file into MongoDB?

If you have CSV files (or TSV files - they're conceptually the same) to import, use the --type=csv or --type=tsv option to tell mongoimport what format to expect. Also important is to know whether your CSV file has a header row - where the first line doesn't contain data - instead it contains the name for each column.

How do I import a CSV file?

On the File menu, click Import. In the Import dialog box, click the option for the type of file that you want to import, and then click Import. In the Choose a File dialog box, locate and click the CSV, HTML, or text file that you want to use as an external data range, and then click Get Data.

How do I import files into MongoDB?

To import JSON file you need to follow the following steps: Step 1: Open a command prompt and give command mongod to connect with MongoDB server and don't close this cmd to stay connected to the server. Step 2: Open another command prompt and run the mongo shell. Using the mongo command.


2 Answers

This should do the trick easily. More HERE.

 mongoimport -d mydb -c collectionName --type csv --file myfile.csv --headerline

Your problem is the <f:\exam.csv bit, which is not properly escaped by the way it looks

> --headerline
> If using “--type csv” or “--type tsv,” use the first line as field names. Otherwise, mongoimport will import the first line as a distinct
> document.
like image 191
flavian Avatar answered Sep 18 '22 13:09

flavian


Please try this line of code

C:\Program Files\MongoDB\Server\3.2\bin>mongoimport -d pravin -c FOC --type csv
--file D:\Script\ImportData\FOC.csv --headerline
2016-08-10T15:42:38.685+0530    connected to: localhost`enter code here`
2016-08-10T15:42:38.758+0530    imported 13 documents
like image 32
Ambrose Pravin Avatar answered Sep 17 '22 13:09

Ambrose Pravin