Normally you would do something like:
var db = new mongo
.Db('test', new mongo.Server('127.0.0.1', 27017), {w: 1})
.open(function (error, database) {
if (error) throw error
console.log('Connected to database test')
})
I, however, want to connect to mongodb synchronously. So I'd need a method I can use in this way:
var db = new mongo.openDb('test', new mongo.Server('127.0.0.1', 27017), {w: 1})
console.log('Connected to database test')
Is there anything like this?
You might want to checkout mongoskin, it connects the database without callback. Here is the example from its github:
var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost:27017/integration_tests", {native_parser:true});
db.bind('article');
db.article.find().toArray(function(err, items) {
db.close();
});
If you use something like node-fibers, then you could do something like what you outlined above.