Connecting to a remote MongoDB instance from ASP.NET

I am having problems with the fact that I cannot connect to the new database that I created in MongoHQ.

I want to include the correct connection string in my Web.config file and bind my Mongo object to the database connection.

Connection strings (edited, obviously):

General access

mongo flame.mongohq.com:27065/dunedin -u {username} -p {password}

AWS Internal Access

mongo flame.local.mongohq.com:27065/dunedin -u {username} -p {password}

Obviously, I included my correct username in the password instead of placeholders in curly braces.

My code is:

string connection = ConfigurationManager.ConnectionStrings["DBMongo"].ConnectionString; var mongo = new Mongo(connection); mongo.Connect(); 

However, as soon as I try to instantiate this Mongo object, I get a format exception saying

Invalid connection string:

What should be this connection string for remote MongoDB instances?

EDIT:

Writing a connection string to Web.Config

 <add name="DBMongo" connectionString="mongo flame.mongohq.com:27065/dunedin -u ausername -p apassword"/> 
+4
source share
4 answers

In mongo shell enter show users . Then use the hashed password that appears for your password in the connection string.

+3
source

The connection string for MongoDB is formatted as a URI, details can be found here . Below is the main format and some examples:

 mongodb://[username: password@ ]host1[:port1][/[database][?options]] mongodb://127.0.0.1 mongodb://127.0.0.1/mydatabase mongodb://mongosrv.com:10230/mydatabase mongodb://myadmin: secretpass@mongosrv.com :10230/mydatabase // Or in your case it would be mongodb://ausername: apassword@flame.mongohq.com :27065/dunedin 

You can also use MongoUrlBuilder and MongoUrl to build or parse the connection string. Although a little verbal, I find the recommended use similar to this.

 var mongoUrl = new MongoUrl(settings.ConnectionString); var mongoClient = new MongoClient(mongoUrl); var mongoServer = mongoClient.GetServer(); var mongoDatabase = mongoServer.GetDatabase(mongoUrl.DatabaseName); 
+6
source

Obviously you are using mongodb-csharp. What you use most specifically is not a valid connection string. You can ask your question in the group http://groups.google.com/group/mongodb-csharp or see the documents and code here. There is even a connection string builder, so you don't need to know the exact syntax.

+1
source

What does your connection string look like in Web.config? The exception you receive indicates that it is invalid. Refine it using the MongoDB documentation.

0
source

Source: https://habr.com/ru/post/1313496/


All Articles