Reputation: 610
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?
Upvotes: 10
Views: 8894
Reputation: 7889
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:
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.
Upvotes: 0
Reputation: 610
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
Upvotes: 27
Reputation: 59783
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)
Upvotes: 1