How to import data into mongoDB - mongodb

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.

Related

How to authenticate with mongoimport to a specific database using Docker and a bash script

I am using a dockerfile and bash script
It should be noted that the mongo db is already running and I can interactively connect to it so this is not an issue.
Dockerfile
FROM mongo
WORKDIR /root
ENV PATH /root/bin:$PATH
WORKDIR /root/bin
COPY import_files/latest.csv .
COPY import_files/field_file_types.txt .
COPY scripts/import.sh .
RUN chmod +x import.sh
CMD sh /root/bin/import.sh
import.sh
#!/bin/bash
ENV_HOST="mongo1"
ENV_USER="user"
ENV_PASS="pass"
ENV_DB="mydb"
ENV_TABLE="mycollection"
# mongoimport -h "${ENV_HOST}:27017" -u "${ENV_USER}" -p "${ENV_PASS}" -c $ENV_TABLE --authenticationDatabase ${ENV_DB} --type csv --file ./latest.csv --columnsHaveTypes --fieldFile=./field_file_types.txt
mongoimport --uri "mongodb://${ENV_USER}:${ENV_PASS}#${ENV_HOST}/${ENV_DB}" -c $ENV_TABLE --type csv --file ./latest.csv --columnsHaveTypes --fieldFile=./field_file_types.txt
exit 0
This is my mongo import statement
mongoimport
-h "mongo1:27017"
-u "user"
-p "pass"
-c "mycollection"
--authenticationDatabase "mydb"
--type csv
--file ./latest.csv
--columnsHaveTypes
--fieldFile=./field_file_types.txt
Which in my console outputs like this:
mongodb://user:pass#mongo1:27017/admin mydb mycollection
And I get error:
error parsing command line options: Invalid Options: Cannot specify different username in connection URI and command-line option ("user" was specified in the URI and "mycollection" was specified in the --username option)
I have also tried it as a uri:
mongoimport --uri "mongodb://user:pass#mongo1/mydb" -c mycollection --type csv --file ./latest.csv --columnsHaveTypes --fieldFile=./field_file_types.txt
Which in my console looks like this:
Mongo import csv = mongodb://user:pass#mongo1:27017/admin mydb mycollection
Enter password:
Why is it asking for password? I provided it as above? Also its exactly the same output as the previous example.
It turns out when I was running docker-compose, I had a script which granted me permission to all databases: (but only if I login as an admin)
mongo <<EOF
use admin
db.createUser(
{
user: "${MONGO_USER}",
pwd: "${MONGO_PASS}",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
use ${MONGO_DB}
db.createCollection("${MONGO_COLLECTION}")
EOF
This means when I connect to mongo I must connect via 'admin' in order to access the other databases. To access the database in question I can include it in the uri string but I must also add -
--authenticationDatabase admin
import script example:
mongoimport --uri "mongodb://user:passt#mongo1/db" -c todos --type csv --file /data/db/latest.csv --columnsHaveTypes --fields "id.auto(),todoText.string(),created_timestamp.date(2006-01-02 15:04:05),completed.date(2006-01-02 15:04:05)" --authenticationDatabase admin

mongoimport from CSV file not assigning object ID's properly

I successfully followed commands to import from csv file to mongodb using CLI.
mongoimport --host myhost --ssl --username my_username --password my_password --authenticationDatabase admin --db my_db --collection addresses --type csv --fields Geocords,Display_lat,Display_lng,street_address,added_by_user --file exportdir2/MFCustomerList.csv
Now the object ID field is not something I have in my file. I expected each row to get its own object ID created on its own as I upload. However instead of just having an object id in there as so: id_:"kalaklakp", each row has something like this in there: id_:ObjectId("gaafagafagafs")
This is causing major problems especially for my Parse instance , It is not able to identify these objects as valid.

mongoDB cant Import a CSV files

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

Upload Data into MongoLab database from terminal

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

unexpected identifier error while bulk insert into mongodb

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.