Search code examples
mongodbconnection

The connection string 'mongodb//localhost' is not valid


Followed Document on how to connect to local host. MongodDB Compass can connect to "localhost:27017", no authentication, no SSL, no SSH Tunnel. In C# project, tried

mongodb//localhost
mongodb//localhost:27017
localhost:27017

all getting the same

MongoDB.Driver.MongoConfigurationException was unhandled
HResult=-2146233088 Message=The connection string 'all-above' is not valid. Source=MongoDB.Driver.Core StackTrace: at MongoDB.Driver.Core.Configuration.ConnectionString.Parse() at MongoDB.Driver.Core.Configuration.ConnectionString..ctor(String connectionString) at MongoDB.Driver.MongoUrlBuilder.Parse(String url) at MongoDB.Driver.MongoUrl..ctor(String url) at MongoDB.Driver.MongoClient..ctor(String connectionString)

Here is the code:

static string con = "mongodb//localhost";
MongoClient mclient = new MongoClient(con);

MongoDB.Driver v2.4.2, MongoDB.Driver.Core v2.4.2

What's wrong?


Solution

    • The " : " is missing after mongodb .
    • In case if you're running on default port, just mongodb://localhost or mongodb://localhost:27017 will do the job.
    • In localhost but on a different port, then mongodb://localhost:diff_port_num
    • In case if you have configured it to other port or mongo server is running on a different server in network the format looks something like this: mongodb://192.168.0.xx:27017