sharp
sharp

Reputation: 2158

Failed: error connecting to db server and mongo shell working

I am trying to import a sample json or csv file into mongodb through command line. I am very new to mongodb and I have tried TONS of stackoverflow questions & answers related to this, however I am unable to solve this problem. I am able to connect to mongo shell just fine. I am able to see list of databases, collections, insert small data through db.collection_name.insert()... etc. This means that the connection is working.

However, when I try to do 'mongoimport' it throws out an error, that 'Failed: error connecting to db server: no reachable servers'. Not sure why the mongoshell is connecting and mongoimport through command line is not working? Thanks!

c0212747agahefweadgadfa:mongo_test user$ mongoimport -h "mongodb+srv://datadatadataname/testdb" --username abcd --db movies --drop --collection ratings --file ratings.json --jsonArray -v
filesize: 10000 bytes
using fields: 
[........................] movies.ratings     0B/939B (0.0%)
[........................] movies.ratings     0B/939B (0.0%)
Failed: error connecting to db server: no reachable servers
imported 0 documents

Upvotes: 1

Views: 718

Answers (1)

tfogo
tfogo

Reputation: 1436

The --host, -h option does not take a scheme (such as mongodb+srv://) or a db name (such as /testdb). See the documentation for the -h flag here: https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption-mongoimport-host

Try:

mongoimport -h host:port --username abcd --db movies --drop --collection ratings --file ratings.json --jsonArray -v

If you find it easier to use a full connection string, you can use the --uri option (https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption-mongoimport-uri).

Upvotes: 1

Related Questions