简体   繁体   中英

How to set mongodb replica set read preference in mongoose?

I am using mongoose to connect mongo(v3.04) replica set, and I want to spread all my requests to all nodes in set. However, after doing like the following, my secondary never got hit, my connection string and options like the following:

let connectionString = 'mongodb://ip1:27017/db, ip2:27017/db';
mongoose.connect(connectionString, {
    server: {
        socketOptions:  {keepAlive: 1},
        readPreference: "nearest",
        strategy: "ping"
    },
    replset: {
        rs_name: 'ReplicaSet',
        socketOptions:  {keepAlive: 1},
        strategy: 'ping',
        readPreference: 'nearest',
        poolSize: 10
    }
});

It looks like the mongoose totally ignore the readPreference settings I passed. I already tried many ways mentioned here, but so far no luck. Anyone could give me a hint?

readPreference: 'nearest' means that clients will ping all replica-set members on connect and address all future reads to the one which responds the fastest. MongoDB clients will usually not switch between primary and secondary randomly.

When you want them to read from the secondary, use readPreference: 'secondaryPreferred' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM