Build a mongo container and import data - mongodb

I want to build a Mongo container, and insert some data stored in the folder /mongo_mock_data in this container. Here are my lines of code:
MONGO_ID=$(docker run --publish-all -d --mount type=bind,source=${PWD}/mongo_mock_data,target=/tmp/mongo_mock_data mongo)
MONGO_PORT=$( docker inspect $MONGO_ID | jq -r '.[0].NetworkSettings.Ports."27017/tcp"[0].HostPort' )
MONGODB_URL=mongodb://localhost:${MONGO_PORT}
docker exec $MONGO_ID mongoimport --host $MONGODB_URL --db algo --collection train --type json --file /tmp/mongo_mock_data/train_data.json --jsonArray
docker exec $MONGO_ID mongoimport --host $MONGODB_URL --db algo --collection calculation --type json --file /tmp/mongo_mock_data/predict_data.json --jsonArray
docker exec $MONGO_ID mongoimport --host $MONGODB_URL --db algo --collection train --type json --file /tmp/mongo_mock_data/cluster_data.json --jsonArray
I have the following error:
error parsing command line options: error parsing URI from mongodb:///localhost:32773/?replicaSet=mongodb:: error parsing uri: must have at least 1 host
any ideas what Im doing wrong?
EDIT:
If I try:
MONGODB_URL=localhost:${MONGO_PORT}
docker exec $MONGO_ID mongoimport --host $MONGODB_URL --db pulse_algo --collection train --type json --file /tmp/mongo_mock_data/train_data.json --jsonArray
instead of
MONGODB_URL=mongodb://localhost:${MONGO_PORT}
docker exec $MONGO_ID mongoimport --host $MONGODB_URL --db algo --collection train --type json --file /tmp/mongo_mock_data/train_data.json --jsonArray
I have the following error:
error connecting to host: could not connect to server: server selection error: server selection timeout

Is the third slash in mongodb:/// a transcription error? If not, then what is happening is that MongoDB is trying to connect to a server at '' (ie, nothing) to a database named localhost:32773, rather than a server at localhost:32773 & the default database.

So, finally, I solved my issue by dropping the argument --host:
MONGO_ID=$(docker run --publish-all -d --mount type=bind,source=${PWD}/mongo_mock_data,target=/tmp/mongo_mock_data mongo)
MONGO_PORT=$( docker inspect $MONGO_ID | jq -r '.[0].NetworkSettings.Ports."27017/tcp"[0].HostPort' )
docker exec $MONGO_ID mongoimport --db algo --collection train --type json --file /tmp/mongo_mock_data/train_data.json --jsonArray
docker exec $MONGO_ID mongoimport --db algo --collection calculation --type json --file /tmp/mongo_mock_data/predict_data.json --jsonArray
docker exec $MONGO_ID mongoimport --db algo --collection train --type json --file /tmp/mongo_mock_data/cluster_data.json --jsonArray

In my case, I was getting this error because of passing option --csv instead of --type=csv to mongoexport. Correction of that fixed this issue.
mongoexport type options
Error message could've been better.

Related

mongoexport not exporting any record from collection

Whats wrong with my command?? It is shows exported 0 records
mongoexport --host maint5-w:27017 -u harry12 -p harry12 --db MTLINKi --collection PROGRAM_HISTORY --type csv --fields L1NAME,TIMESPAN --out zips.csv

mongoimport Docker Failed: error connecting to the db server: no reachable servers

I am trying to create a Docker container with MongoDB and import data into it. I have tried using the following dockerfile:
FROM mongo
# This will be created if it doesn't exist
WORKDIR /app/data/
# Copy dependency definitions
COPY mydata.csv .
ENTRYPOINT mongod
# Import data
RUN mongoimport --host=127.0.0.1 -d mydb -c reports --type csv --file mydata.csv --headerline
I get the following error:
Failed: error connecting to the db server: no reachable servers
Any suggestions? Thanks!
Try this :
mongoimport --host 127.0.0.1 --port <specifyPort> -d mydb -c reports --type csv --file mydata.csv --headerline

How to take mongodump for 1 collection from my Database in MongoDB

How to take mongodump for 1 collection from my Database
../mongodump --db db_name --collection collection_name --out /home/dell/999/
i got this error
bash: ../mongodump: No such file or directory
This is working for entire db backup
./mongodump --out /home/dell/777/ --db dbname
But back up for single collection from a database not working
Use mongoexport to export collection data:
mongoexport --db test --collection mycollection --out myCollection.json
If it's a replica set and you want to use the --uri you should use it like this cause documentation states that you can't specify some options when using --uri
mongodump --uri "mongodb://user:password#mongo-en-1.example.io:27017,mongo-en-2.example.io:27017,mongo-en-3.example.io:27017/$Databasename?replicaSet=$replicasetname&authSource=admin" --collection $collectionname

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

can not find unexpected undentifier in mongo db export query

I am writing this query in mongo shell
mongoexport --db database_name --collection collection_name --out filename.json
query to export but the error is
SyntaxError: unexpected identifier
can anybody help me ?
Mongoexport is a terminal command line utility to export data, not mongoshell functional directive.
Try running the same in terminal.
$ mongoexport --db database_name --collection collection_name --out filename.json
Make sure you have all the utilities that come with mongo added to your path.