Question

Is there any way to define read prefence in mongoskin in collection or db level here is the sample from mongodb native nodejs driver http://learnmongodbthehardway.com/ex22.html

var readMongo = require("mongodb").MongoClient;  

MongoClient.connect('localhost:3000/tes?readPreference=secondaryPreferred' , 

   function(err ,db){
        db.collection("sites").findOne(
        {
            $or: [{'a' : e}, 
                    {'a' : d},
                    {'a' : c},
                    {'a' : b}]
        }, function(error, result1){
            if(error){
                console.log(error);
            } else {
                callback(null, result1)
                db.close();
            }       
        });
});

}

I am currently running mongos over a set of sharding server with replica sets. I need to route all the read query to secondary members of replica sets.

Était-ce utile?

La solution

Finally there is way to route your query to secondary mongod

Just place the host and port of your secondary inside the mongoskin connection string like this


var mongo = require("mongoskin");
var database = mongo.db( "mongod://username:password@secondaryDatabasehost:port/database",{safe:true , slaveOk : true});

The slaveOk:true will allow secondary mongod to use for read only queries.Without this mongo wont allow to query from a secondary and gives you an error.

[Mongo error "$err" : "not master" ]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top