I spent several hours tracking this. The problem came down to my use of Mongoose. I used Mongoose schemas for some of my Collections and did not use Mongoose for others. Here is the file containing my problematic code:
// Some common MongoDb operations and data. 'use strict'; var MongoClient = require('mongodb').MongoClient; var assert = require('assert'); var logger = require('./Logger'); var mongoose = require('mongoose'); // TODO: Take this out for production. mongoose.set('debug, true'); var PSSharingInvitations = require('./PSSharingInvitations') var connectedDb = null; // Call this just once, at the start of the server. // TODO: Need better error handling when can't initially connect. Right now have an ugly looking error when Mongo is not already started and we try to start our server. exports.connect = function(mongoDbURL) { MongoClient.connect(mongoDbURL, function(err, db) { assert.equal(null, err); if (!db) { logger.error("**** ERROR ****: Cannot connect to MongoDb database!"); } else { mongoose.connect(mongoDbURL); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function() { // SCHEMA's exports.SharingInvitation = PSSharingInvitations.buildSchema(mongoose); logger.info("Mongoose: Connected to MongoDb database"); }); connectedDb = db; logger.info("Mongo: Connected to MongoDb database"); } }); }; exports.db = function () { return connectedDb; }; // Call this just once, when the server shuts down. exports.disconnect = function() { };
The problem turned out to be direct:
connectedDb = db;
Where db was mongoose.connection . That is, I used mongoose.connection as my db for MongoDB collections that did not use Mongoose. This caused intermittent errors.
The revised (and still working!) Code is as follows:
exports.connect = function(mongoDbURL) { MongoClient.connect(mongoDbURL, function(err, db) { assert.equal(null, err); if (!db) { logger.error("**** ERROR ****: Cannot connect to MongoDb database!"); } else { connectedDb = db; logger.info("Mongo: Connected to MongoDb database"); mongoose.connect(mongoDbURL); var connectedMongooseDb = mongoose.connection; connectedMongooseDb.on('error', console.error.bind(console, 'connection error:')); connectedMongooseDb.once('open', function() {
source share