How to manage mongodb connections in a nodejs webapp?

I'm using node-mongodb-native driver with mongodb to write a website.

I have some questions about how to manage connections:

  1. Is it enough using only one mongodb connection for all requests? Is there any performance issues? If not, can I setup a global connection to use in the whole application?

  2. If not, is it good if I open a new connection when request arrives, and close it when handled the request? Is it expensive to open and close a connection?

  3. Should I use a global connection pool? I hear the driver has a native connection pool, is it a good choice?

  4. If I use a connection pool, how many connections should be used?

  5. Is there other things I should notice?

Thanks for helping~

The primary committer to node-mongodb-native says:

You open do MongoClient.connect once when your app boots up and reuse the db object. It's not a singleton connection pool each .connect creates a new connection pool.

So, to answer your question directly, reuse the db object that results from MongoClient.connect(). This gives you pooling, and will provide a noticeable speed increase as compared with opening/closing connections on each db action.

I have been using generic-pool with redis connections in my app - I highly recommend it. Its generic and I definitely know it works with mysql so I don't think you'll have any problems with it and mongo

https://github.com/coopernurse/node-pool

If you have Express, you can use express-mongo-db for caching and sharing mongodb connection between requests without pool (since accepted answer says it is the right way to share the connection).

If not - you can look on it's source code and use it in other framework.

http://mongoosejs.com/docs/api.html

Check out the source of Mongoose. They open a connection and bind it to a Model object so when the model Object is required, a connection is made to the DB. The driver takes care of connection pooling.