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();
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);
}