user2357929
user2357929

Reputation: 51

how to import csv file in 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

Upvotes: 5

Views: 24626

Answers (4)

Krishna
Krishna

Reputation: 1

Run mongoimport from the system command line, not the mongo shell.

Ref - https://docs.mongodb.com/manual/reference/program/mongoimport/

Upvotes: 0

Andrea D&#39;Aguanno
Andrea D&#39;Aguanno

Reputation: 21

I solved by opening the .csv file in Excel (File -> Options -> advanced) then unchecking the box "uses system separation" and then removing the comma from the box below and then saving again in .csv.

enter image description here

So there will not be any commas in the .csv file and the formatting of JSON in MongoDB will be right.

Upvotes: 1

Ambrose Pravin
Ambrose Pravin

Reputation: 11

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

Upvotes: 1

flavian
flavian

Reputation: 28511

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.

Upvotes: 9

Related Questions