Search code examples
node.jsmongodbrestify

What's the best practice for MongoDB connections on Node.js?


This is something that is a bit unclear to me (I'm just getting started with Node and Mongo), and it really concerns me because of server performance and strain (which I guess is another question, but I'll get to that at the end of the post).

So, assuming I'm writing an API with Node.js and Restify, where each API endpoint corresponds to a function, should I:

a) open the db connection and store it in a global var, and then just use that in every function?
Example:

// requires and so on leave me with a db var, assume {auto_reconnect: true}
function openDB() {
    db.open(function(err, db) {
        // skip err handling and so on
        return db;
    }
}

var myOpenDB = openDB(); // use myOpenDB in every other function I have

b) open the db connection and then just put everything in one giant closure?
Example:

// same as above
db.open(function(err, db) {
    // do everything else here, for example:
    server.get('/api/dosomething', function doSomething(req, res, next) { // (server is an instance of a Restify server)
        // use the db object here and so on
    });
}

c) open and close the db each time it is needed?
Example:

// again, same as above
server.get('/api/something', function doSomething(req, res, next) {
    db.open(function(err, db) {
        // do something
        db.close();
    });
});

server.post('/api/somethingelse', function doSomethingElse(req, res, next) {
    db.open(function(err, db) {
        // do something else
        db.close();
    });
});

This last one is what I would do out of intuition, but at the same time I don't feel entirely comfortable doing this. Doesn't it put too much strain on the Mongo server? Especially when (and I hope I do get to that) it gets hundreds — if not thousands — of calls like this?

Thank you in advance.


Solution

  • I like MongoJS a lot. It lets you use Mongo in a very similar way to the default command line and it's just a wrapper over the official Mongo driver. You only open the DB once and specify which collections you'll be using. You can even omit the collections if you run Node with --harmony-proxies.

    var db = require('mongojs').connect('mydb', ['posts']);
    
    server.get('/posts', function (req, res) {
      db.posts.find(function (err, posts) {
        res.send(JSON.stringify(posts));
      });
    });