Understanding WriteConcern in MongoDB C#

≡放荡痞女 提交于 2019-12-12 10:44:40

问题


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.


回答1:


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.




回答2:


For 2.x c# driver, you can use write concern in the following way:

var collection = db.GetCollection<Record>(collectionName)
    .WithWriteConcern(new WriteConcern(
        w: 1,
        wTimeout: default(TimeSpan?),
        fsync: true,
        journal: false));

then any updates to the db using this collection will use the passed write concern.

collection.InsertOne(...);
collection.ReplaceOne(...);
collection.UpdateMany(...);
and so on

There are several predefined write concerns e.g.

for very fast but unreliable updates:

var collection = db.GetCollection<Record>(collectionName)
    .WithWriteConcern(WriteConcern.Unacknowledged);

or for WriteConcern which is similar to the default (w=1)

var collection = db.GetCollection<Record>(collectionName)
    .WithWriteConcern(WriteConcern.W1);

or for acknowledge of majority members of the replica set

var collection = db.GetCollection<Record>(collectionName)
    .WithWriteConcern(WriteConcern.WMajority);

for details and more options please see the documentation here: https://mongodb.github.io/mongo-csharp-driver/2.7/apidocs/html/T_MongoDB_Driver_WriteConcern.htm



来源:https://stackoverflow.com/questions/26660950/understanding-writeconcern-in-mongodb-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!