I have been reading up on Write Concern in MongoDB. I understand that there are several levels that determine the level of guarantee that a write operation succeeded, with a performance trade-off for the higher you set this level. However, I am working in a C# environment, and I am trying to figure out how to use Write Concern there and what levels work best for certain situations. I already figured out how to collect the result of the check with a WriteConcernResult object, I am mainly interested in the levels themselves.
These are my questions:
How do I set the Write Concern level in C# for specific writes?
This answer suggests using the connection string, but this looks like a global setting, which I don't want, since some of the write operations I will be using are more "important" than others and I don't want to kill the performance. I noticed there's a WriteConcern class but the documentation isn't very detailed about its use (it's under MongoDB.Driver Namespace in the documentation).
In particular, how do I set it to "Journaled" or "Replica Acknowledged", seeing as it's "Acknowledged" by default?
What types of issues could get past the Write Concern check for each level?
For example: system crashes, power failures, network connection issues, etc. I am especially interested in something sneaking by that is not easily detected, as power failures and the like are very noticeable and we could estimate the time interval where operations may have failed and react accordingly.
The operations in the MongoDB C# driver have overloads that accept a WriteConcern
that you can get by 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);
This for example requires 3 replicas on top of the primary, hence W4
, the Journal flag is turned on and the wtimeout is set to 100 ms.