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

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)