MongoDB usage best practices

本文提供关于MongoDB使用的最佳实践、驱动程序说明、连接模式、异常处理等关键信息,帮助开发者提高学习效率,避免常见错误,确保代码的清洁与可靠性。

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


原文地址:http://weblogs.asp.net/andresv/archive/2012/10/24/mongodb-usage-best-practices.aspx

先展示下效果 https://pan.quark.cn/s/5061241daffd 在使用Apache HttpClient库发起HTTP请求的过程中,有可能遇到`HttpClient`返回`response`为`null`的现象,这通常暗示着请求未能成功执行或部分资源未能得到妥善处理。 在本文中,我们将详细研究该问题的成因以及应对策略。 我们需要掌握`HttpClient`的运作机制。 `HttpClient`是一个功能强大的Java库,用于发送HTTP请求并接收响应。 它提供了丰富的API,能够处理多种HTTP方法(例如GET、POST等),支持重试机制、连接池管理以及自定义请求头等特性。 然而,一旦`response`对象为`null`,可能涉及以下几种情形:1. **连接故障**:网络连接未成功建立或在请求期间中断。 需要检查网络配置,确保服务器地址准确且可访问。 2. **超时配置**:若请求超时,`HttpClient`可能不会返回`response`。 应检查连接和读取超时设置,并根据实际需求进行适当调整。 3. **服务器故障**:服务器可能返回了错误状态码(如500内部服务器错误),`HttpClient`无法解析该响应。 建议查看服务器日志以获取更多详细信息。 4. **资源管理**:在某些情况下,如果请求的响应实体未被正确关闭,可能导致连接被提前释放,进而使后续的`response`对象为`null`。 在使用`HttpClient 3.x`版本时,必须手动调用`HttpMethod.releaseConnection()`来释放连接。 而在`HttpClient 4.x`及以上版本中,推荐采用`EntityUtils.consumeQuietly(respons...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值