arturovm
arturovm

Reputation: 1235

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.

Upvotes: 39

Views: 17385

Answers (2)

juandopazo
juandopazo

Reputation: 6329

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

Upvotes: 14

scttnlsn
scttnlsn

Reputation: 3026

  • Option A is not a great idea since there is no guarantee that the DB will be finished opening before an HTTP request is handled (granted this is very unlikely)
  • Option C is also not ideal since it needlessly opens and closes the DB connection

The way that I like to handle this is using deferreds/promises. There are a bunch of different promise libraries available for Node but the basic idea is to do something like this:

var promise = new Promise();

db.open(function(err, db) {
    // handle err
    promise.resolve(db);
});

server.get('/api/something', function doSomething(req, res, next) {
    promise.then(function(db)
        // do something
    });
});

I believe Mongoose handles connections in a way that vaguely resembles this.

Upvotes: 6

Related Questions