MongoDBC # driver

Source: Internet
Author: User
Tags mongoclient mongodb server
I read the Bson class library before reading the C # Driver. Now I want to learn C # DriverThreadsafety (multithreading problem). Only a small part of the C # Driver Class is multithreading secure. For example, MongoClient, MongoServer, MongoDatabase, MongoCollection, and MongoGridFS. Common classes have multithreading problems, including javascurs.

Reading Notes C # Driver I read the Bson class library before. Now I want to learn C # Driver Thread safety (multithreading problem). Only a small part of the C # Driver Class is multithreading secure. For example, MongoClient, MongoServer, MongoDatabase, MongoCollection, and MongoGridFS. Common classes have multithreading problems, including javascurs.

Reading Notes

C # Driver

I read the Bson class library and now I want to learn C # Driver.

Thread safety (multithreading problem)

Only a small part of the C # Driver Class is multithreading secure. For example, MongoClient, MongoServer, MongoDatabase, MongoCollection, and MongoGridFS. The commonly used classes have multithreading problems, including the wide cursor and all the classes in the Bson class library (except that BsonSymbolTable is thread-safe ).

The static attribute values and Function Methods of all classes do not cause multithreading.

Consumer Client class

This class provides the basic object for using MongoDB server. When you connect to the MongoDB server Service, the client automatically connects. (The connection pool is used for more effective connections)

When a replica set is connected, only one Consumer Client instance is used.

When you are connecting to a replica set you will still use only one instance of your client, which represents the replica set as a whole. the driver automatically finds all the members of the replica set and identifies the current primary.

Instances of this class will not cause multithreading issues.

Unless otherwise specified, one WriteConcern is required for all operations and one write statement is required. In addition, by default, all write operations are locked until the server knows that the write operation is required.

Connection strings

The simplest database Connection is to use Connection string. The standard Connection string is as follows:

Mongodb: // [username: password @] hostname [: port] [/[database] [? Options]

Username and password must be entered on the mongodb server that uses the authentication.

The port number is optional. The default value is 27017.

To connect to multiple servers, you can enter multiple server names (and the required port number) and separate them with commas. As follows:

Mongodb: // server1, server2: 27017, server2: 27018

The connection string above connects to three database services. Because the multi-database service is ambiguous, it cannot determine whether the service is a replica set or a multi-database service. The drive will skip the connection string syntax check and directly connect to the database server so that the server can check their category by itself. There are also some ways to specify the category of the data server during the connection, that is, to directly describe it in the connection string. As follows:

Mongodb: // server1, server2: 27017, server2: 27018 /? Connect = replicaset

Available connection modes include: automatic (default), direct, replica set, and shardrouter. The connection rules are as follows:

1. If a connection mode is specified, use it directly. Otherwise, use the default automatic.

2. If there is a replica set name in the connection string, the replica set mode is used.

3. If there is only one server in connection string, direct mode is used.

4. After connecting to the service, the Service determines the connection mode.

Note: if there is a multi-server list Connection, one of which is a replica set, and the other is not, the connection mode will become non-deterministic (not determined ). Make sure that there is no hybrid service type in connection string.

When the connection mode is specified as the replica set, the server in the U.S. is located, but the driver interface still finds the primary server, even if the server is not in the connection list. Until a server in the connection list responds (this response includes the replica set and the existing primary service ). In addition, even after the initialization statement is complete, other secondary servers will be discovered and automatically added to the hybrid cluster. In this way, if you add or delete a Hong Kong server and move the replica set, the driver interface will handle these changes by itself.

By the way, suppose you want to directly connect to a replica set and check whether it is the current primary (maybe you just want to monitor its running status or make a read-only statement ), you can use the following connection statement:

Mongodb: // server2 /? Connect = direct; readpreference = nearest

You can obtain a complete set of connection string documents at the following link.

Depth:

SSL Support

These are not of interest. They are probably a driver connection setting.

Add the "ssl = true" option to connection string to set

Mongodb: // server2 /? Ssl = true

By default, the server is licensed by a local trusted Certificate Authority. In some test environments, the test server has not signed a certificate. to alleviate this problem, the Hong Kong Virtual Host, you can add "sslverifycertificate = false" to the connection string to shield all certificate errors (authentication errors ).

Authentication

MongoDB supports two authentication methods. One is to call a specific method during program execution. When a specific method is executed, authentication will be used. Another robust method is to store authentication information in MongoCredentialsStore.

The following is an example. credential store is used to determine the authentication information of the admin AND foo databases. In addition to using "admin" and "foo" to connect to the database, you can also use the default authentication "test ".

Var url = new response url ("mongodb: // test: user @ localhost: 27017"); var settings = clientclientsettings. fromUrl (url); var adminCredentials = new inclucredentials ("admin", "user", true); settings. credentialsStore. add ("admin", adminCredentials); var fooCredentials = new inclucredentials ("foo", "user", false); settings. credentialsStore. add ("foo", fooCredentials); var client = new guest client (settings); I feel like an SQL statement: Foo. *; GetServer method calls the GetServer method in the Consumer Client instance to obtain the consumer server instance. MongoServer class

You can use the volume server class to perform more control operations. It uses advanced technologies to obtain databases and perform a series of database operations through a single socket, and maintains database consistency.

GetDatabase method

Access the database through this method

Sample Code:

Login client = new Login client (); // connect to localhostlogin server = client. getServer (); your database test = server. getDatabase ("test"); your credentials = new your credentials ("username", "password"); your database salaries = server. getDatabase ("salaries", credentials );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.