A wrapper for MongoDB's MongoClient to open the connection only once and reopen it only if closed by the server


A wrapper for MongoDB's MongoClient that handles keeping the connection open and dealing with reconnections.

In Node.js applications MongoDB connections are meant to be opened once and kept opened while the Node.js app is running (

This module encapsulates the logic required to do this.


cd yourApp
npm install mongoconnect

Typical usage is

In your main server.js

var mongoConnect = require('./mongoConnect');

In your database access code (say dbTest.js)

var mongoConnect = require('./mongoConnect');
mongoConnect.execute(function(err, db) {
  // Your code to do something with the db goes here
  // e.g. db.collection('xxx').find(...)


Be aware that currently only one database can be handled through this code. As such, only the first call to setup is honored, subsequent calls are ignored.