Connect synchronously to mongodb

You can’t connect to MongoDB synchronously, but you may get rid of this ugly callback from your code.

The best way to do it is to adopt some wrapper around node-mongodb-native driver.

Take a look at the following modules.


mongojs

var mongojs = require('mongojs');
var db = mongojs('localhost/test');
var mycollection = db.collection('mycollection');

mongoskin

var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost:27017/test", {native_parser:true});

monk

var monk = require('monk');
var db = monk('localhost/test');
var users = db.get('users')

Of course, internally all of them are establishing MongoDB connection asynchronously.

Leave a Comment