How to get a instance of db from node-mongo native driver? How to get a instance of db from node-mongo native driver? mongoose mongoose

How to get a instance of db from node-mongo native driver?


You could write a wrapper, a new module where you store the db instance, something similar to this:

//db.jsvar HOSTNAME = ...var PORT = ...var db = module.exports = {};var instance;db.connect = function (){    ...    instance = <db_instance>;};db.disconnect = function (){    ...    instance = null;};db.instance = function (){    return instance;};

Now, every time you need the db instance retrieve it by doing:

var db = require ("./path/to/db");db.instance ();