开发者

Using MongoDB in a web environment efficiently

I'm approaching MongoDB from an NHibernate background and I want to know what the best practices are for efficient usage in a web context.

With NHibernate, I create a single ISessionFactory for the life of the application, then use an instance of an ISession per request. Take the below code for example (which i hope is typical, please correct if its not ideal).

Would I typically have a single instance of the Mongo class per application, or per request? What about var db? Or do I do use all the code below whenever I want DB interaction?

Update: I'm using mongodb-csharp (although please suggest a better alternative if it exists)

Thanks

using (var mongo = new Mongo())
{
    mongo.Connect();
开发者_StackOverflow中文版
    var db = mongo.GetDatabase("mydb");

    var mongoCollection = db.GetCollection("mycollection");

    var document = new Document(Guid.NewGuid().ToString(), new
                                                    {
                                                        x = 1,
                                                        y = 2
                                                    });

    mongoCollection.Insert(document);
}


Each of the drivers typically have some form of persistent connection or connection pooling. I'm not sure which language / driver you're using, but check your docs for connection pooling.

Also, when running Mongo.connect() you'll typically have connection string (mongodb://user:pwd@host:port/db), which will cut down on the lines of code and get you straight to the collection.

Also, you generally don't need to use GUID. Mongo drivers generally provide some form of "MongoID" which is specific to Mongo. In addition, the default behaviour is to create an ID for you if you don't have one.

Other than that, I would look at your driver / library in detail as they are slightly different.


When using mongodb-csharp you treat it like you would an ADO connection. When you create a Mongo object it borrows a connection from the pool, which it owns until it is disposed. So after the using block the connection is back into the pool. Creating Mongo objects are cheap and fast.

Example

for(var i=0;i<100;i++) 
{ 
        using(var mongo1 = new Mongo()) 
        using(var mongo2 = new Mongo()) 
        { 
                mongo1.Connect(); 
                mongo2.Connect(); 
        } 
} 

Database Log
Wed Jun 02 20:54:21 connection accepted from 127.0.0.1:58214 #1
Wed Jun 02 20:54:21 connection accepted from 127.0.0.1:58215 #2
Wed Jun 02 20:54:21 MessagingPort recv() errno:0 No error 127.0.0.1:58214
Wed Jun 02 20:54:21 end connection 127.0.0.1:58214
Wed Jun 02 20:54:21 MessagingPort recv() errno:0 No error 127.0.0.1:58215
Wed Jun 02 20:54:21 end connection 127.0.0.1:58215

Notice it only opened 2 connections.

I put this together using mongodb-csharp forum. http://groups.google.com/group/mongodb-csharp/browse_thread/thread/867fa78d726b1d4

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜