Well, I lost time trying to figure it out. In the end, after some consideration and under the influence of John Pap 's code , I decided to use the database module as follows:
var Q = require('q'); var MongoClient = require('mongodb').MongoClient; module.exports.getDb = getDb; var db = null; function getDb() { return Q.promise(theDb); function theDb(resolve, reject, notify) { if (db) { resolve(db); } else { MongoClient.connect(mongourl, mongoOptions, function(err, theDb) { resolve(db); } }); } } }
So, when I need to execute the request:
getDb().then(function(db) {
At least for Mongodb, this is a good practice, as shown here .
source share