Search code examples
node.jsmongoosenode-mongodb-native

Should MongooseJS be emitting events on replica set disconnection?


With a single server setup, I receive events from the driver.

mongoose.connect('mongodb://localhost/mydb');
mongoose.connection.on('disconnected', function() {...});
mongoose.connection.on('error', function(err) {...});

When using a replica set (mongoose.connect('mongodb://localhost:27017/mydb,mongodb://localhost:27018/mydb');), shutting down all connected set members doesn't trigger those same events.

I'm not very familiar with the internals of the native driver and I'm wondering if this is a bug or if I need to manually detect this condition.

I'm using Mongoose 3.6.17 (mongodb driver 1.3.18)

Sans mongoose, I tried this with the same results (no events from a replica set).

require('mongodb').MongoClient.connect("mongodb://localhost:27017,localhost:27018/mydb", function(err, db) {
    if (db) {
        db.on('disconnected', function() {
            console.log('disconnected');
        }).on('error', function(err) {
            console.log('error');
        });
    }            
});

Solution

  • I've been having similar problems with Mongoose, asked on SO also. More recently, I've found this issue on Mongoose GitHub repository which led to this issue on the driver repository.

    The Mongo driver wasn't emitting any of these events more than once, and today this has been fixed for single connections on v1.3.19.
    It seems that it's a "won't fix" for now.