gremo
gremo

Reputation: 48439

Why MongoDB Node.js driver does not complain about a wrong replica set?

I'm just curious about the fact that the native driver doesn't complain about my (not properly configured) replica set. In fact my setup has just one instance of mongod listening on port 27021 (ports 27018 and 27019 ar not even used):

var async          = require('async')
  , mongodb        = require('mongodb')
  , Db             = mongodb.Db
  , Connection     = mongodb.Connection
  , Server         = mongodb.Server
  , ReplSetServers = mongodb.ReplSetServers;

async.waterfall([
    function (callback) {
        var rls = new ReplSetServers([
            new Server('localhost', 27017, {}), // Just mongod instance
            new Server('localhost', 27018, {}), // Offline
            new Server('localhost', 27019, {}), // Offline
        ]);

        new Db('test', rls, { w: 0 }).open(function (err, db) {
            callback(err, db);
        });
    },
], function (err, db) {
    if (err) console.error(err);
    if (db) db.close();
});

No errors outputted. Of course stopping the only instance of mongod running causes a connection error. Am I'm missing something obvious here?

Upvotes: 0

Views: 748

Answers (1)

James Wahlin
James Wahlin

Reputation: 2821

The list of servers provided to the driver is used to discover your replica set members. If the driver can connect to at least one replica, it will perform an rs.isMaster() call (you can try this in the shell) which returns a list of active members. As long as the operations performed do not violate the current state of the set (like trying to write when there is no primary) then you won't see an error. Even for the write, you will have to ask for acknowledgement (w: >= 1) to see the error on the client.

Upvotes: 1

Related Questions