How to connect to a slave if master goes down — Replication


I'm playing around with data replication in MongoDb, and I currently have a master and several slave nodes. All of these are running on different ports, but they could just as well be on different machines (I'm testing atm).

My question is, how would I go about connecting to these from my code? Currently, I just have one instance of a db class, which is connected to x port on localhost. Obviously, if this node fails, I want to use the slave at y port on localhost, and if that fails, z port on localhost. Shall I use separate db objects for different connections?

By : Matthew H

Okay, I've looked through the driver source code, and it looks like this is the way to configure clusters:

var db = new mongo.Db('test', new mongo.ServerCluster([new mongo.Server(host, 27018, {}),
                                                    new mongo.Server(host, 27019, {}), 
                                                    new mongo.Server(host, 27017, {})]), {});
By : Matthew H

This video can help you solving your question :)
By: admin