mongoexport not exporting any record from collection - mongodb

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

Related

Build a mongo container and import data

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.

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

export csv from MongoDB

I am new to MongoDB. I want to export some fields to csv file and if that field is present in particular row then I want empty value in that field. Currently I am trying this:
mongoexport --host hostname --collection collectionname -q '{}' -f "field1","field2" --db dbname --username user --password pass --out out.csv
But problem is that output does not keep the field if field value is not present in the database. Any suggestion how can I perform the desired operation?
Try:
mongoexport --host hostname --username user --password pass --db dbname --collection collectionname --type=csv --fields field1,field2 --query '{field1: { $exists: true}, field2: { $exists: true}}' --out out.csv
For more detail: Click here

why mongoexport behaves this way?

when I run this,
mongoexport --db mydb_dev --collection location_updates --csv --fields lat,lon --out location.csv
it gives me,
connected to: 127.0.0.1
exported 0 records
However when I move --db to the end and run this,
mongoexport --collection location_upates --csv --fields lat,lon --out location.csv --db mydb_dev
it gives me,
connected to: 127.0.0.1
exported 92913 records
Any idea why it works like this? Is there ordering of Options important in Mongo?

mongoexport --fields --csv will only output the first field when working with subdocuments

Working:
.\mongoexport.exe --db mydb --collection slideproof_user_event_date_count --csv --out
events.csv --fields '_id,first_day'
.\mongoexport.exe --db mydb --collection slideproof_user_event_date_count --out
events.json --fields '_id._p,first_day'
Not working (only first field/column has content) written, :
.\mongoexport.exe --db mydb --collection slideproof_user_event_date_count --csv --out
events.csv --fields '_id._p, first_day'
How can I enable correct output for .csv with subdocument fields?
The solution is to avoid spaces in the --fields argument:
this works:
--fields '_id._p,value.count'
This does not:
--fields '_id._p, value.count'