5

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

Said Kaldybaev
  • 9,380
  • 8
  • 36
  • 53
user2357929
  • 51
  • 1
  • 1
  • 2
  • http://stackoverflow.com/questions/4686500/how-to-use-mongoimport-to-import-csv – ymn May 07 '13 at 10:45
  • 1
    Also, as the parameter suggests, it's supposed to be a comma-separated-value file, not semi-colons. – WiredPrairie May 07 '13 at 11:12
  • 1
    your file is not "comma seperate" rather it is semi comma seperated (;). change it to Comma Seperate using excel if using windows or use sed command in UNIX and then import it. – Sushil May 07 '13 at 12:00
  • Please [Check Here](http://docs.mongodb.org/manual/core/import-export/) , this link give you the idea – Rohit Vyas May 07 '13 at 10:47

4 Answers4

9

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.
flavian
  • 28,161
  • 11
  • 65
  • 105
  • How passing the file as an argument instead of reading it from the standard input should help? – toro2k May 07 '13 at 11:44
1

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
1

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.

beso9595
  • 1,146
  • 3
  • 10
  • 16
0

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

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

Krishna
  • 1
  • 1
  • 1