WriteConcern必须在哪里实现?

时间:2016-03-07 15:14:56

标签: c# mongodb mongodb-.net-driver

这是一个代码段,我将PLC数据插入MongoDB,但我不确定WriteConcern必须在哪里实现?

    var connectionString = "mongodb://10.52.124.186:27017/";

    // Establish connection from the client to the server 
    var client = new MongoClient(connectionString);

    var server = client.GetServer();

    // Connect to the MongoDB specified for the GDS on the Mongodb
    var mongoDB = server.GetDatabase("test_database");

    // create a collection called sample
    var collection = mongoDB.GetCollection<sample>("sample");

    sample a = new sample();

    // Access the socket via which PLC has sent the data
    a.Parameter = data;

    collection.Insert(a);

1 个答案:

答案 0 :(得分:1)

您可以在几个级别上声明所需的默认WriteConcern级别。

  • 在MongoClient对象的设置上
    • 将MongoSettings对象传递给构造函数
    • 或稍后设置mongoClient.Settings.WriteConcern
  • 通过设置database.Settings.WriteConcern
  • 在数据库级别上
  • 通过设置collection.Settings.WriteConcern
  • 在集合级别上
  • 在每个数据库查询中使用带有WriteConcern对象的方法重载。

当然,每个设置都可以被较低级别的不同设置覆盖。那么你选择哪个级别取决于你想做什么。