I have a json file and am trying to insert in bulk to mongodb
I do
mongoimport --host localhost --db testdb --collection testdbjson --username user -- password pass --type json --file /home/pet/mng/json_test.json
It gives the following error
SyntaxError: Unexpected identifier
Please help!
the document mongoimport the correct syntax
mongoimport -host 127.0.0.1 -port 27017 -u user -p pass -collection testdbjson --db testdb --file /home/pet/mng/json_test.json
or
mongoimport -host 127.0.0.1:27017 -u user -p pass -collection testdbjson --db testdb --file /home/pet/mng/json_test.json
I had the exact same error now - I figured it out. I think you have connected to your mongo instance on mongolab first before you wanted to import.
You must not connect to mongolab first - disconnect by typing in "exit" in the shell to get back to command prompt. Then issue the mongoimport command again, the import should work this time.
Related
I'm trying to import a large data file in JSON format . I'm using
mongoimport --db verbs --collection de --file "/Users/marcelbraasch/Downloads/de.json"
to import the data. This is going through, however, I'm getting the following exception:
Failed: (Unauthorized) not authorized on verbs to execute command { insert: "de", ordered: false, writeConcern: { w: "majority" }, $db: "verbs" }
I already tried combinations like this
mongoimport -h localhost:27017 -u 'user' -p 'password' --db verbs --collection de --file "/Users/myname/Downloads/de.json"
but none of it worked. My mongo instance is running in a docker container, if this information matters. What do I need to do?
Found the answer here. This missing keyword was authenticationDatabase. The command that worked for me was:
mongoimport --db verbs --collection de --authenticationDatabase admin --username user --password password --drop --file /Users/myname/Downloads/de.json.
I was struggling with the same issue, adding keyword --authenticationDatabase worked for me.
I am trying to import a CSV file to MongoDB following this command:
mongoimport -d Northwind -c "employee-territories" --type csv --file "/home/ubuntu/Downloads/northwind-mongo-master/employee-territories.csv" --headerline
but I've got the following error message
"Syntax Error: missing ; before statement (shell):1"
to solve this error I have to use mongoimport OUTSIDE the mongoshell environment.
So I just turn terminal on
Unfortunately I've got the error message
error inserting documents: not authorized on Northwind to execute command { insert: "employee-territories", writeConcern: { getLastError: 1, w: 1 }, ordered: false, $db: "Northwind" }
what's wrong with my code ?? please help
Looks like, that you forgot to provide the authentication data (username, password and authenticationDatabase).
Your command should look like that:
mongoimport -u "your username" -p "yoyr password" --authenticationDatabase "admin" -d Northwind -c "employee-territories" --type csv --file "/home/ubuntu/Downloads/northwind-mongo-master/employee-territories.csv" --headerline
Take a look into mongoimport documentation.
thank you for your reply, But after adding the user name and password the following error appeared
" Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed "
To resolve this error I added --host and --authenticationDatabase parameters and it worked while we connecting to a remote MongoDB
Use the similar below format in your code:
mongoimport --host 127.0.0.1:27017 -u "admin" -p "admin123" --authenticationDatabase admin -d Northwind -c "employee-territories" --type csv --file "/home/ubuntu/Downloads/northwind-mongo-master/employee-territories.csv" --headerline
I'm having trouble figuring out how to upload csv data to my MongoLab database. From my terminal I have used
sudo mongoimport --db heroku_hkr86p3z -u <dbusername> -p <dbpassword> --collection contributors --type csv --headerline --file /Users/tonywinglau/Desktop/independent-expenditure.csv
and
sudo mongoimport --host mongodb://<username>:<password>#ds035310.mlab.com:35310/heroku_hkr86p3z --db heroku_hkr86p3z -u <username> -p <password> --collection contributors --type csv --headerline --file /Users/tonywinglau/Desktop/independent-expenditure.csv
both of which respond with
Failed: error connecting to db server: no reachable servers
imported 0 documents
From what I have read it might be something to do with my 'mongo config' file (I can't find it if it does exist) being set to only connect with localhost? How do I import data directly into my mongolab hosted database?
Your command line should look like this:
mongoimport -d <databasename> -c <collectionname> --type csv --file <filelocation/file.csv> --host <hostdir example:ds011291.mlab.com> --port <portnumber example:11111> -u <username> -p <password> --headerline
The host direction and the port number it gived by mlab when you create the database.
Example:
ds000000.mlab.com:000000/databaseName
I'm facing a problem in importing a CSV file to my db. I tried this command:
mongoimport --db meteor -h localhost:3001 -c TollPlaza -d meteor --headerline --type csv --file TollPlaza.csv
I referred to this question but am still having a problem.
Try this query :
mongoimport --db meteor --host localhost --port 3001 -c TollPlaza -d
meteor --headerline --type csv --file TollPlaza.csv
Maybe seperate host and port
The query is perfectly fine.
Follow the instructions
run the meteor project
Open a one more command prompt in same location
make sure that csv file present in the same directory.
run the query.
check the DB (show collections).
Dumped a MongoDB successfully:
$ mongodump -h ourhost.com:portnumber -d db_name01 -u username -p
I need to import or export it to a testserver and have struggle with it, please help me figure out.
I tried some ways:
$ mongoimport -h host.com:port -c dbname -d dbname_test -u username -p
connected to host.
Password: ...
Gives this error:
assertion: 9997 auth failed: { errmsg: "auth fails", ok: 0.0 }
$ mongoimport -h host.com:port -d dbname_test -u username -p
Gives this error:
no collection specified!
How to specify which collection to use? What should I use for -d? What I'd like to upload or what I want to use as test out there? I would like to import the full DB not only collection of it.
The counterpart to mongodump is mongorestore (and the counterpart to mongoimport is mongoexport) -- the major difference is in the format of the files created and understood by the tools (dump and restore read and write BSON files; export and import deal with text file formats: JSON, CSV, TSV.
If you've already run mongodump, you should have a directory named dump, with a subdirectory for each database that was dumped, and a file in those directories for each collection. You can then restore this with a command like:
mongorestore -h host.com:port -d dbname_test -u username -p password dump/dbname/
Assuming that you want to put the contents of the database dbname into a new database called dbname_test.
You may have to specify the authentication database
mongoimport -h localhost:27017 --authenticationDatabase admin -u user -p -d database -c collection --type csv --headerline --file awesomedata.csv
For anyone else might reach this question after all these years (like I did), and if you are using
a dump which was created using mongodump
and trying to restore from a dump directory
and going to be using the default port 27017
All you got to do is,
mongorestore dump/
Refer to the mongorestore doc for more info. cheers!
When you do a mongodump it will dump in a binary format. You need to use mongorestore to "import" this data.
Mongoimport is for importing data that was exported using mongoexport