Arjun
Arjun

Reputation: 19

Where must be WriteConcern implemented?

This is a code segment where I will insert PLC data into MongoDB but I am not sure as to where must WriteConcern be implemented?

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

Upvotes: 1

Views: 538

Answers (1)

Philipp
Philipp

Reputation: 69683

There are several levels on which you can state your desired default WriteConcern level.

  • On the Settings of the MongoClient object
    • either by passing a MongoSettings object to the constructor
    • or by setting mongoClient.Settings.WriteConcern later
  • On database level by setting database.Settings.WriteConcern
  • On collection level by setting collection.Settings.WriteConcern
  • On every single database query by using the method overloads which take a WriteConcern object.

Each of these settings can of course be overridden by a different setting on a lower level. So which level you choose does depend on what you want to do.

Upvotes: 1

Related Questions