문제

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.

도움이 되었습니까?

해결책

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" ]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top