Understanding WriteConcern in MongoDB C #

I read Record Concern in MongoDB. I understand that there are several levels that determine the level of guarantee that a write operation succeeds, with a compromise in performance so that you set this level higher. However, I work in a C # environment, and I'm trying to figure out how to use Write Concern there and which levels are best suited for certain situations. I already understood how to collect the test result using the WriteConcernResult object, I am interested in the levels themselves.

These are my questions:

How to set record level in C # for specific records?

This answer suggests using a connection string, but it looks like a global parameter that I don't want, as some of the write operations that I will use are more “important” than others, and I don't want to kill the play. I noticed the WriteConcern class there , but the documentation does not describe its use in very detail (it is in the MongoDB.Driver namespace in the documentation).

In particular, how can I set it to “Logged” or “Confirmed Copy”, seeing that the default is “Confirmed”?

What types of problems can pass record verification for each level?

For example: system crashes, power failures, network connectivity issues, etc. I am particularly interested in something stalking that is not easily detected , as there are power failures, etc. very noticeable, and we can estimate the time interval in which operations can fail and respond accordingly.

+6
source share
1 answer

Operations in the C # MongoDB driver have overloads that accept WriteConcern , which you can get using the class constructor or using a predefined static property:

 var writeConcern = WriteConcern.W4; writeConcern.Journal = true; writeConcern.WTimeout = TimeSpan.FromMilliseconds(100); new MongoClient().GetServer().GetDatabase("").GetCollection("").Insert(null, null, writeConcern); 

For this, for example, 3 replicas are required over the primary, therefore W4 , the log flag is on, and the wtimeout time is 100 ms.

+1
source

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


All Articles