2

I'm only used to writing small scale web applications, and [at work] we're building something for our customers that we anticipate will be pretty data-intensive.

We've chosen MongoDB because the of the loose schema system we need but I'm a little confused about how MongoDB handles connections. The way I was always taught was open a connection, perform a CRUD operation, then close the connection. But the MongoDB docs say you should never need to use the close connection function in normal circumstances. So do I need to be creating all these Mongo client objects and stuff?

What are the best practices to use here? This is my first time using a database engine that isn't relational.

Adam
  • 311
  • 1
  • 3
  • 15

1 Answers1

1

That could vary based on your driver (the client library, which is different for every language).

In the C# driver at least, you don't need to create more than a single client, and use it throughout the application. You don't even need to create a connection, it's all internal and managed for you.

Take for example this extremely simple code. It creates a client, gets a database, gets a collection inside it and retrieves all documents in that collection (and if one of them doesn't exist, it will create them):

var client = new MongoClient();
var hamsters = client.GetServer().GetDatabase("HamsterSchool").GetCollection("Hamsters").FindAll();
i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • 1
    @Adam I'm not an expert in `PHP`, but looking at the [Tutorial](http://www.php.net/manual/en/mongo.tutorial.php) it looks exactly the same as in `C#`. – i3arnon Jun 23 '14 at 10:45