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: http://www.mongodb.org/display/DOCS/CSharp+Driver+Quickstart

C# Language Center: http://www.mongodb.org/display/DOCS/CSharp+Language+Center

MongoDB Server Documentation: http://www.mongodb.org/display/DOCS/Home

MongoDB Server Downloads: http://www.mongodb.org/downloads

MongoDB client drivers download: http://www.mongodb.org/display/DOCS/Drivers

MongoDB Community content: http://www.mongodb.org/display/DOCS/CSharp+Community+Projects

Tutorials

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: http://www.mongodb.org/display/DOCS/getLastError+Command

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: http://www.mongodb.org/display/DOCS/CSharp+getLastError+and+SafeMode

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: http://stackoverflow.com/questions/4604868/mongodb-c-sharp-safemode-official-driver

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 https://groups.google.com/forum/?fromgroups#!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: http://ethangunderson.com/blog/two-reasons-to-not-use-mongodb/

FourSquare using MongoDB had serious scalability problems: http://mashable.com/2010/10/07/mongodb-foursquare/

Is MongoDB a replacement for Memcached? http://www.quora.com/Is-MongoDB-a-good-replacement-for-Memcached/answer/Rick-Branson

MongoDB Introduction, shell, when not to use, maintenance, upgrade, backups, memory, sharding, etc: http://www.markus-gattol.name/ws/mongodb.html

MongoDB Collection level locking support: https://jira.mongodb.org/browse/SERVER-1240

MongoDB performance tips: http://www.quora.com/MongoDB/What-are-some-best-practices-for-optimal-performance-of-MongoDB-particularly-for-queries-that-involve-multiple-documents

Lessons learned migrating from SQL Server to MongoDB: http://www.wireclub.com/development/TqnkQwQ8CxUYTVT90/read

MongoDB replication performance: http://benshepheard.blogspot.com.ar/2011/01/mongodb-replication-performance.html

No Comments