This document describes the connection between the URI format and the application and the MongoDB instance. For details, refer to the official MongoDB driver.
Original article: http://docs.mongodb.org/manual/reference/connection-string/#connections-connection-options
Standard connection string format
Connect MongoDBURI in the standard format described in this section to connect to the MongoDB database server. All official MongoDB drivers are in the same format. For the list of Drivers and Drivers, see the MongoDB driver and Client library (MongoDB Drivers and Client Libraries)
The following are standard links
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
Parameter description:
Mongodb: // standard format of string Link
Username: password @
Optional. Generally, there is no name or password by default. It only appears when the MongoDB server uses authentication.
/Database
Optional. The database is required if the link string contains the user name and password for authentication. If the database name is not included, the admin database is connected by default.
? Options
Link specific options. If no specific database name is specified for the link, you must add "/" to the Host Name and start with a question mark before optin "? ".
Instance:
Mongodb: // db1.example.net, db2.example.net: 2500 /? ReplicaSet = test
Another article was found. Only focus on Translation:
Internal Authentication
When you only need one user, it is possible to specify it in the connection string.
Var connectionString = "mongodb: // user1: password1 @ localhost/test"; var login client = newlogin client (connectionString); Note: if you do not specify a database connection string, the default database is the "admin" database.
I was so excited that I could finally set the configuration file, but I found that the c # driver does not support such writing, so it is a loss.
Link principle:
Link to the database. The official website provides two thread-Safe Methods: Producer client and consumer server, which are thread-safe and automatically locked.
Link a database using the server class
// Create server settings to passconnection string, timeout, etc.
MongoServerSettingssettings = newdomainserversettings ();
Settings. Server = new hosted serveraddress ("localhost", 27017 );
// Create server object tocommunicate with our server
Login server = new Login server (settings );
// Get our databaseinstance to reach collections and data
Var database = server. GetDatabase ("MessageDB ");
Client-class connected database instance
var client = new MongoClient("mongodb://localhost:27017");var server = client.GetServer();var database = server.GetDatabase("foo");var collection = database.GetCollection("bar");
I will test both of the above two link methods. And can be used. This is generally more inclined to client connection.
By searching the Api, we can find that there are other methods to construct the client class.
public MongoClient(); public MongoClient(MongoClientSettingssettings); public MongoClient(MongoUrl url); public MongoClient(stringconnectionString);
We usually use a string link. In this in-depth search for clientsettings, sending the clientsettings call allows more parameter settings, which is similar to Option, the solution is found, and the clientsettings parameter is set through the configuration file.
By default, the maximum connection pool is 100, the minimum is 0, and the database connection is local. Next let's look at the test
Required clientsettings settingsclient = newclientclientsettings (); // instantiate the client setting class
The red dashes are the default parameters. When we do not enter any parameters, the driver sets the default parameters.
The parameter settings of the client class can be found to be different.
How is the code written?
# Region read configuration file information // obtain the link pool size int connectionPool = Convert. toInt32 (ConfigurationManager. appSettings ["connectionPool"]); int minpool = Convert. toInt32 (ConfigurationManager. appSettings ["minpool"]); string hostname = ConfigurationManager. appSettings ["hostname"]; Int32 port = Convert. toInt32 (ConfigurationManager. appSettings ["port"]); string database = ConfigurationManager. appSettings ["database"]; # endregion if (String. isNullOrEmpty (ConnectionString) {throw newArgumentNullException ("Connection string not found. ") ;}# set serveraddress ipaddress = newdomainserveraddress (hostname, port) for the region client class; // set the Server ip address and port MongoClientSettings settingsclient = new MongoClientSettings (); // instantiate the client setting class settingsclient. server = ipaddress; // port value settingsclient. maxConnectionPoolSize = connectionPool; settingsclient. minConnectionPoolSize = minpool; settingsclient. connectionMode = 0; // link mode setting // MongoUrl url = newMongoUrl (ConnectionString); MongoClient client = newclientclient (settingsclient ); // call the client constructor to set the parameter MongoServer server = client. getServer (); // The server obtains the client parameter DB = server. getDatabase (database); // obtain the database name # endregion
At the beginning, I didn't know how to assign values to parameters, but I found that parameters have types in step-by-step operations. Some types are some reference classes and they need to be instantiated and assigned values. So there are many parameters that seem to end up.
Information in the configuration file
<Addkey = "connectionPool" value = "1000"/> <! -- Connection pool settings --> <add key = "hostname" value = "192.168.24.249"/> <add key = "port" value = "27017"/> <add key = "database" value = "DB3"/> <add key = "minpool" value = "300"/> </appSettings>
Of course, the client method is written. The server is much easier.
# Region server link settings: Configure serversettings using setting = new using serversettings (); // using setting. server = newdomainserveraddress (ConnectionString, connectionPool); Using setting. maxConnectionPoolSize = connectionPool; // you can specify the maximum number of connections in the connection pool. server = newdomainserveraddress (hostname, port); your server Server server = your Server. create (export setting); // Create a connection data file DB = server. getDatabase (database); # endregion
Cognition:
Because the database has few references, most of the materials are in foreign languages. At first, I was surprised to see that so much English can be understood. But as long as the mood is calm, everything is okay. Through Basic Search and some basic settings learned by foreign language forums. Similar to SQL settings, but different, code settings are required. This is the code parameter that can be written in the configuration file. I deeply understand that English is a very important tool. If you want to learn more, it is an essential tool.