Alex Rivera | Logout

How do I use SafeMode with the MongoDB C# driver

Asked 2011-01-05T14:02:15.960
15

I found that some methods of the official MongoDB C# driver use SafeMode and return SafeModeResult. What is this SafeMode and how do I use it? It would be great to see some use cases - for example, a use case with the RemoveAll method of a MongoCollection.

Edit
Report

1 Answer

24

From documentation:

There are various level of SafeMode, and this class is used to represent those levels. SafeMode applies only to operations that don't already return a value (so it doesn't apply to queries or commands). It applies to the following MongoCollection methods: Insert, Remove, Save and Update.

The gist of SafeMode is that after an Insert, Remove, Save or Update message is sent to the server it is followed by a GetLastError command so the driver can verify that the operation succeeded. In addition, when using replica sets it is possible to verify that the information has been replicated to some minimum number of secondary servers.

The SafeMode class has static properties and methods that let you easily access common modes or create your own:

* SafeMode.False
* SafeMode.True
* SafeMode.WaitForReplications(int n)

The value for "n" includes the primary, so typically you want n >= 2.

I hope this is enough to understand the purpose of SafeMode.

answered 2011-01-05T14:20:31.240

Your Answer