MongoDB usage best practices

The project I'm working on uses MongoDB for some stuff so I'm creating some documents to help developers speedup the learning curve and also avoid mistakes and help them write clean & reliable code.

This is my first version of it, so I'm pretty sure I will be adding more stuff to it, so stay tuned!

C# Official driver notes

The 10gen official MongoDB driver should always be referenced in projects by using NUGET. Do not manually download and reference assemblies in any project.

C# driver quickstart guide:

C# Language Center:

MongoDB Server Documentation:

MongoDB Server Downloads:

MongoDB client drivers download:

MongoDB Community content:


Safe Mode Connection

The C# driver supports two connection modes: safe and unsafe. Safe connection mode (only applies to methods that modify data in a database like Inserts, Deletes and Updates.

While the current driver defaults to unsafe mode (safeMode == false) it's recommended to always enable safe mode, and force unsafe mode on specific things we know aren't critical.

When safe mode is enabled, the driver internal code calls the MongoDB "getLastError" function to ensure the last operation is completed before returning control the the caller. For more information on using safe mode and their implicancies on performance and data reliability see:

If safe mode is not enabled, all data modification calls to the database are executed asynchronously (fire & forget) without waiting for the result of the operation. This mode could be useful for creating / updating non-critical data like performance counters, usage logging and so on. It's important to know that not using safe mode implies that data loss can occur without any notification to the caller.

As with any wait operation, enabling safe mode also implies dealing with timeouts. For more information about C# driver safe mode configuration see:

The safe mode configuration can be specified at different levels:

  • Connection string: mongodb://hostname/?safe=true
  • Database: when obtaining a database instance using the server.GetDatabase(name, safeMode) method
  • Collection: when obtaining a collection instance using the database.GetCollection(name, safeMode) method
  • Operation: for example, when executing the collection.Insert(document, safeMode) method

Some useful SafeMode article:

Exception Handling

The driver ensures that an exception will be thrown in case of something going wrong, in case of using safe mode (as said above, when not using safe mode no exception will be thrown no matter what the outcome of the operation is).

As explained here!topic/mongodb-user/mS6jIq5FUiM there is no need to check for any returned value from a driver method inserting data. With updates the situation is similar to any other relational database: if an update command doesn't affect any records, the call will suceed anyway (no exception thrown) and you manually have to check for something like "records affected".

For MongoDB, an Update operation will return an instance of the "SafeModeResult" class, and you can verify the "DocumentsAffected" property to ensure the intended document was indeed updated.

Note: Please remember that an Update method might return a null instance instead of an "SafeModeResult" instance when safe mode is not enabled.

Useful Community Articles

Comments about how MongoDB works and how that might affect your application:

FourSquare using MongoDB had serious scalability problems:

Is MongoDB a replacement for Memcached?

MongoDB Introduction, shell, when not to use, maintenance, upgrade, backups, memory, sharding, etc:

MongoDB Collection level locking support:

MongoDB performance tips:

Lessons learned migrating from SQL Server to MongoDB:

MongoDB replication performance:

No Comments