10

Trying to import or export from MongoDB command line :

> mongoimport --db denistest --collection things --type csv --file C:/Users/Administrator/Desktop/csv_data.csv

I get :

JavaScript execution failed: SyntaxError: Unexpected identifier

What is wrong here?

user4157124
  • 2,809
  • 13
  • 27
  • 42
Denis Omeri
  • 610
  • 1
  • 9
  • 21
  • What do a few lines of your CSV file look like? – WiredPrairie Mar 28 '13 at 10:43
  • "denis","omeri","21","Tirana","1","http:/google.com","m" "olgert","llojko","20","Prrenjas","2","http:/facebook.com","m" – Denis Omeri Mar 28 '13 at 10:50
  • If you just try those lines, does it work? Did you happen to try this suggestion: http://stackoverflow.com/questions/4686500/how-to-use-mongoimport-to-import-csv – WiredPrairie Mar 28 '13 at 10:56
  • I made a new csv file with just those two lines and still not working. Other commands (like show databases, use, find ect) are successfully executed. – Denis Omeri Mar 28 '13 at 11:04
  • Do you have a line with the field names? – WiredPrairie Mar 28 '13 at 11:08
  • I have tried it after adding column names in the first line of csv but still same problem. I thinks problem is in this line: `> mongoimport --db denistest --collection things --type csv --file C:/Users/Administrator/Desktop/csv_data.csv` – Denis Omeri Mar 28 '13 at 11:16

3 Answers3

27

I finally found the problem. I was executing my command in mongo.exe. ( I opened C:\mongodb\bin>mongo and the executed my command )

C:\mongodb\bin>mongo
MongoDB shell version: 2.4.0
connecting to: test
>
> mongoimport --db denistestcsv --collection things --type csv --fields First,La
st,Visits,Location,Number,Url,Letter --file E:\temp\csv1.csv

But this is wrong. The correct way is going to mongo => bin => and then executing command without entering mongo.exe

C:\mongodb\bin>mongoimport --db denistestcsv --collection things --type csv --fi
elds First,Last,Visits,Location,Number,Url,Letter --file E:\temp\csv1.csv
Denis Omeri
  • 610
  • 1
  • 9
  • 21
1

Using your sample data, saved into a file called csv1.csv:

"denis","omeri","21","Tirana","1","http:/google.com","m" 
"olgert","llojko","20","Prrenjas","2","http:/facebook.com","m"

I ran the following command line (split for readability here, with made-up field names):

mongoimport --db test 
    --collection things 
    --type csv 
    --fields First,Last,Visits,Location,Number,Url,Letter 
    --file d:\temp\csv1.csv

And it imports successfully:

connected to: 127.0.0.1
Thu Mar 28 07:43:53.902 imported 2 objects

And in the things DB:

> db.things.find()
{ "_id" : ObjectId("51543b09d39aaa258e7c12ee"), 
     "First" : "denis", "Last" : "omeri", "Visits" : 21, 
     "Location" : "Tirana", 
     "Number" : 1, "Url" : "http:/google.com", "Letter" : "m" }
{ "_id" : ObjectId("51543b09d39aaa258e7c12ef"), 
     "First" : "olgert", "Last" : "llojko", "Visits" : 20, 
     "Location" : "Prrenjas", 
     "Number" : 2, "Url" : "http:/facebook.com", "Letter" : "m" }

(I couldn't get the header row option working in 2.4 for CSV files for some reason, but the option of specifying the fields on the command-line works as well. You can also use a file that contains just the field names by using the fieldFile command line option)

WiredPrairie
  • 58,954
  • 17
  • 116
  • 143
0

Building off of Dennis Omeri's answer, I had a similar problem trying to make a remote connection to a mongolab MongoDB instance via the CLI. I had to run the command from the bin folder of MongoDB as well, but it's not as straightforward as his answer makes it seem.

In my case, I installed mongodb via homebrew on OSX (brew install mongodb assuming you have homebrew installed). That put my mongo system files in: /usr/local/cellar/mongodb/3.0.3/bin (sub in your version of mongo and the path should be good).

Then, locate the following items:

  • mongolab URL (should be something like ds123456-a.mongolab.com)
  • port number (probably 27799 or 2####)
  • database user (you will have to set this up in mongolab)
  • database password (set this up in mongolab as well)

You can then connect to mongolab using the following string
mongo ds123456.mongolab.com:27799/dbname -u dbuser -p dbpassword

Also note: in my case, I had setup mongolab via a Heroku add-on. This means you have to authenticate through the heroku dashboard to gain access to your mongolab db user/pw setup area. None of this is really documented anywhere I could find, so I'm adding this answer in to help the next soul who wanders into this mess.

serraosays
  • 7,163
  • 3
  • 35
  • 60