Reputation: 381
I am reading WriteConcern
at mongoDB wiki but it's not clear for me. I have a question! what is it and when we must use of WithWriteConcern(WriteConcern.Acknowledged)
?
what is difference between:
WithWriteConcern(WriteConcern.Acknowledged).InsertOne()
and InsertOne()
and which one is better that we use?
please explain simple.
Upvotes: 6
Views: 5660
Reputation: 151
sayres, the write concern is an specification of MongoDB for write operations that determines the acknowledgement you want after a write operation has taken place. MongoDB has a default write concern of always acknowledging all writes, which means that after every write, MongoDB has to always return an acknowledgement (in a form of a document), meaning that it was successful. When asking for write acknowledgement, if none isn't returned (in case of failover, crashes), the write isn't successful. This behavior is very useful specially on replica set usage, since you will have more than one mongod instance, and depending on your needs, maybe you don't want all instances to acknowledge the write, just a few, to speed up writes. Also, when to specify a write concern, you can specify journal writing, so you can guarantee that operation result and any rollbacks required if a failover happens. More information, here.
In your case, it depends on how many mongod (if you have replica sets or just a single server) instances you have. Since "always acknowledge" is the default, you may want to change it if you have to manage replica sets operations and speed things up or just doesn't care about write acknowledgement in a single instance (which is not so good, since it's a single server only).
Upvotes: 9