When trying to import or export from command line (MongoDB): For Ex:
> mongoimport --db denistest --collection things --type csv --file C:/Users/Administrator/Desktop/csv_data.csv
i am getting "JavaScript execution failed: SyntaxError: Unexpected identifier" error?
What is wrong here?
So, to export data from the MongoDB database, MongoDB provides a command-line tool known as mongoexport. Using this tool you can exports data of a collection in JSON or CSV(comma-separated value) format. Moreover, we can also use features like limit and sort on a collection while exporting the data.
You can use the mongoimport command to import CSV files into a collection using the headerline option. Headerline option notifies the mongoimport command of the first line; to be not imported as a document since it contains field names instead of data.
Export MongoDB to CSV (e.g. Excel) Open the Export Wizard and select your export source. This screen only appears if you haven't chosen an item in the Connection Tree, run a previous query, or selected specific documents. Next, choose CSV as the export format then click Next.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With