了解WriteConcern MongoDB中C#WriteConcern、MongoDB

2023-09-04 10:55:01 作者:波板塘

我一直在阅读上撰写关注 MongoDB中。据我所知,有几个的水平,确定保证写操作成功的水平,性能权衡设置此级别越高。然而,我的工作在C#环境,我试图找出如何使用写有关注,什么层次的工作最适合某些情况。我已经想通了如何收集与WriteConcernResult对象的检查结果,我主要感兴趣的是水平本身。

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.

这些是我的问题:

如何在C#中的特定写入设置写入关注程度?

这个答案建议使用连接字符串,但是这看起来像一个全局设置,这是我不希望的,因为有些我会用写操作更重要不是别人,我不想杀人的性能。我注意到有一个 WriteConcern类但文档是不是它的使用非常详细的(它的下MongoDB.Driver命名空间的文档中)。

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?

什么类型的问题可能让过去写支票关注每个级别?

例如:我在用的东西是偷偷特别感兴趣的系统崩溃,电源故障,网络连接问题等。不容易发现,如电源故障等是非常明显的,我们可以预计的时间间隔中的操作可能已经失败,并作出相应的反应。

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.

推荐答案

在MongoDB的C#驾驶员的操作有接受重载一个 WriteConcern ,您可以用得到类的构造函数或使用predefined静态属性:

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);

此,例如,需要在主顶部3复制品,因此 W4 ,日刊标志开启且wtimeout被设置为100毫秒。

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.