Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 298
arturovm Avatar asked Aug 03 '12 16:08

arturovm


People also ask

Which method is used to connect from Nodejs to MongoDB?

No-SQL databases allow developers to send and retrieve data as JSON documents, instead of SQL objects. To work with MongoDB in a Node. js app, we can use Mongoose.

Is MongoDB good for node JS?

Node and MongoDB work very-well together, in part because Mongo uses a JavaScript engine built into the database since JavaScript is good at handling JSON objects. Compared to other databases, such as MySQL, MongoDB is fast for storing certain types of data and can be automatically scaled.

Which drivers are useful to connect node js with MongoDB?

Connect your Node. js applications to MongoDB and work with your data using the Node. js driver. The driver features an asynchronous API that you can use to access method return values through Promises or specify callbacks to access them when communicating with MongoDB.

When should you use Nodejs and when should you use MongoDB?

There are many web servers built with nodejs that will then use MongoDB for storing data. MongoDB offers an API library that runs within a Nodejs application to give you programmatic access to MongoDB so you can create databases and then add, query, update or delete data from the MongoDB database.


2 Answers

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));   }); }); 
like image 94
juandopazo Avatar answered Oct 06 '22 03:10

juandopazo


  • 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.

like image 26
scttnlsn Avatar answered Oct 06 '22 02:10

scttnlsn