Home Database Mysql Tutorial MongoDB C# 驱动

MongoDB C# 驱动

Jun 07, 2016 pm 05:56 PM
mongodb drive

读书笔记 C# Driver 之前看了Bson类库,现在学习C# Driver Thread safety(多线程问题) 只有少部分的C# Driver类是多线程安全的。比如MongoClient,MongoServer,MongoDatabase, MongoCollection 以及MongoGridFS。一般常用的类存在多线程问题,包括MongoCurs

读书笔记

C# Driver

之前看了Bson类库,现在学习C# Driver

Thread safety(多线程问题)

只有少部分的C# Driver类是多线程安全的。比如MongoClient,MongoServer,MongoDatabase, MongoCollection 以及MongoGridFS。一般常用的类存在多线程问题,包括MongoCursor以及Bson类库中的所有类(除了其中的BsonSymbolTable是线程安全的)。

所有的类的静态属性值和函数方法都不会引起多线程问题。

MongoClient类

这个类提供使用MongoDB server的基本对象。与MongoDB server服务进行链接的时候,client自动进行连接。(使用了连接池来进行更有效的连接)

在连接一个副本集的时候,有且只用一个MongoClient实例。

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

这个类的实例不会引起多线程问题。

除非其他设置,在默认设置情况下,所有操作需要一个WriteConcern,一个写入确定语句。另外,默认情况下,所有的写操作会锁定,直到server知道要进行写操作。

Connection strings

最简单的数据库连接是使用Connection string。标准的Connection string如下:

mongodb://[username:password@]hostname[:port][/[database][?options]]

在使用认证的mongodb服务器上,username和password必须填写。

port号码是可选的。默认的是27017.

如果要连接多个服务器,可以直接填写多个服务器名(以及需要的端口号),并且以‘,’分割。如下:

mongodb://server1,server2:27017,server2:27018

上面这段connection string 连接了三个数据库服务,由于多数据库服务是模糊不清的,不能分辨服务是否复本集,或者是多数据库服务。drive驱动会跳过connection string的语法检查,直接连接进数据库服务器,让server自己检查他们的类别。还有一些办法在连接的时候就指定数据服务器的类别,就是在connection string里面直接描述。如下:

mongodb://server1,server2:27017,server2:27018/?connect=replicaset

可用的连接模式包括:automatic (默认), direct, replica set, 以及shardrouter。连接的规则如下:

1、如果指定了某种连接模式,则直接使用否则使用默认的automatic。

2、如果在connection string中有replica set name,则使用replica set模式

3、如果connection string中仅有一个服务器,则使用direct模式

4、另外,连接服务之后,服务决定连接的模式

注意:如果有多服务器列表连接,其中有一个是复本集的一个,而其他不是,则连接模式将成为non-deterministic(未决定)。确定connection string中没有混合服务类型。

当连接模式指定成为replica set,美国服务器,但是driver接口还是会找到primary服务器,即使该服务器不在connection连接列表中。直到connection列表中的一个服务器的回应(这个回应包括replica set以及现有的primary服务)。另外,即使在初始化语句完成之后,其他次级服务器也会被发现,并且自动加入到混合集群。这样,香港服务器,如果你有添加以及删除,移动replica set,driver接口会自己处理这些改变。

顺便提到,假设你想要直接连接入一个replica set并且无论它是否是现在的primary(也许只是想监控下它的运行状态或者进行只读语句),可以使用下面连接语句:

mongodb://server2/?connect=direct;readpreference=nearest

可以在下面的链接获取比较齐全的connection string文档

更加深入地额:

SSL Support

这些不感兴趣,大概是driver连接的一个设置

通过在connection string里面加入“ssl=true”选项来设置

mongodb://server2/?ssl=true

在默认的情况下,server是通过本地的受信任的证书机构获取许可。在一些测试环境下面,测试server没有签署证书,为了缓解这个情况,香港虚拟主机,可以使用在connection string里面添加“sslverifycertificate=false”来屏蔽所有certificate errors(认证错误)。

Authentication

MongoDB支持两种认证方式。一种是在程序执行时,调用特定的方法。在执行特定的方法时,认证将会被使用。另外一种健壮的方法是在MongoCredentialsStore存储认证信息。

下面是一个例子,使用credential store来确定admin和“foo”数据库的认证信息。除了使用“admin”以及“foo”连接入数据库,还可以使用默认的认证“test”。

var url = new MongoUrl("mongodb://test:user@localhost:27017"); var settings = MongoClientSettings.FromUrl(url); var adminCredentials = new MongoCredentials("admin", "user", true); settings.CredentialsStore.Add("admin", adminCredentials); var fooCredentials = new MongoCredentials("foo", "user", false); settings.CredentialsStore.Add("foo", fooCredentials); var client = new MongoClient(settings); 我感觉类似SQL语句: foo.* ; GetServer method 在MongoClient实例中调用GetServer方法获取MongoServer的实例。   MongoServer class  

使用MongoServer类可以进行更多的控制操作。它使用了先进的技术通过一个单个的socket获取数据库以及进行一系列的数据库操作,并且保持数据库的一致性。

GetDatabase method

通过这个方法访问数据库

例子代码:

MongoClient client = new MongoClient(); // connect to localhost MongoServer server = client.GetServer(); MongoDatabase test = server.GetDatabase("test"); MongoCredentials credentials = new MongoCredentials("username", "password"); MongoDatabase salaries = server.GetDatabase("salaries", credentials);
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Does Logitech ghub driver not support win7? -Why can Logitech ghub driver only be installed on the c drive? Does Logitech ghub driver not support win7? -Why can Logitech ghub driver only be installed on the c drive? Mar 18, 2024 pm 05:37 PM

Does Logitech ghub driver not support win7? Not compatible. Since Windows 7 has stopped updating and is no longer Microsoft's main operating system, many new software no longer supports it, such as Logitech ghub. The main interface of the Logitech driver: 1. The main software interface is on the left. The three buttons are lighting, buttons, and sensitivity settings. 2. In the settings of the lighting interface, the general special effects are relatively conventional, and the audio visual effects are the highlight. They can change color according to the sound frequency, and can be set according to the high, middle and bass bands, with different colors and effects. 3. In button settings, users can edit them here according to their special requirements. 4. In the sensitivity settings, many users will have some of their own settings. They can add the DPI speed switching point by themselves, but

Steps to restore Logitech driver to default configuration Steps to restore Logitech driver to default configuration Feb 28, 2024 am 11:04 AM

Facing the Logitech driver that cannot be used normally, it can only be solved by restoring the factory settings. However, many friends do not know how to restore the factory settings. The following will provide you with detailed steps to restore the factory settings. I hope it can help you. Steps to restore the Logitech driver to the default configuration: 1. First download the Logitech dedicated driver software GHub, and open it after the download is complete. 2. Then open the mouse to set up, and click the settings (gear) in the upper right corner. 3. At the bottom, click "Restore default settings" , click directly and reopen the software to restore factory settings. How to connect the Logitech driver to the device 1. Open the back cover of the mouse and take out the wireless mouse socket. 2. Select the mouse socket next to the computer. 3. Set the corresponding parameter information for the mouse. 4. Bluetooth pair the device and long press Bluetooth.

Which version is generally used for mongodb? Which version is generally used for mongodb? Apr 07, 2024 pm 05:48 PM

It is recommended to use the latest version of MongoDB (currently 5.0) as it provides the latest features and improvements. When selecting a version, you need to consider functional requirements, compatibility, stability, and community support. For example, the latest version has features such as transactions and aggregation pipeline optimization. Make sure the version is compatible with the application. For production environments, choose the long-term support version. The latest version has more active community support.

How to download Razer mouse driver How to download Razer mouse driver Mar 11, 2024 pm 03:40 PM

Steps to download the Razer mouse driver: 1. Open the browser and enter the Razer official website; 2. On the official website page, find and click "Technical Support" or a similar option; 3. On the technical support page, select "Mouse" or the specific subcategory; 4. On the mouse driver download page, you can see various mouse models and their corresponding drivers; 5. Click the download link for the selected driver; 6. After the download is completed, check whether the downloaded file is complete , make sure nothing is damaged or missing.

The difference between nodejs and vuejs The difference between nodejs and vuejs Apr 21, 2024 am 04:17 AM

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

How to install win11 driver without digital signature_Tutorial on how to deal with win11 driver without digital signature How to install win11 driver without digital signature_Tutorial on how to deal with win11 driver without digital signature Mar 20, 2024 pm 04:46 PM

Some users have encountered some problems when installing drivers for win11 computers. The computer prompts that the digital signature of this file cannot be verified, resulting in the inability to install the driver. How to solve this problem? Please see the following introduction for details. 1. Press the [Win + [Ctrl+Shift+Enter] Open the Windows Powershell window with administrator rights; 3. User Account Control window, do you want to allow this application to make changes to your device? Click [Yes]; 4. Administrator: Windows Powers

Where is the database created by mongodb? Where is the database created by mongodb? Apr 07, 2024 pm 05:39 PM

The data of the MongoDB database is stored in the specified data directory, which can be located in the local file system, network file system or cloud storage. The specific location is as follows: Local file system: The default path is Linux/macOS:/data/db, Windows: C:\data\db. Network file system: The path depends on the file system. Cloud Storage: The path is determined by the cloud storage provider.

What are the advantages of mongodb database What are the advantages of mongodb database Apr 07, 2024 pm 05:21 PM

The MongoDB database is known for its flexibility, scalability, and high performance. Its advantages include: a document data model that allows data to be stored in a flexible and unstructured way. Horizontal scalability to multiple servers via sharding. Query flexibility, supporting complex queries and aggregation operations. Data replication and fault tolerance ensure data redundancy and high availability. JSON support for easy integration with front-end applications. High performance for fast response even when processing large amounts of data. Open source, customizable and free to use.

See all articles