Mongoose: Read on ReplicaSet

后端 未结 6 1969
暖寄归人
暖寄归人 2021-01-02 21:57

I have a mongodb replica set from which I want to read data from primary and secondary db.

I have used this command to connect to the db:

mongoose.con

6条回答
  •  时光取名叫无心
    2021-01-02 22:42

    Mongoose use node package "mongodb", connection uri or opts is parsed by "mongodb". Here is mongodb connect opts and mongodb readPreference source code.

    So, we can use mongoose like this:

    var opts = {db: {readPreference: 'nearest'}; mongoose.connect(uri, opts);

    Also, just use uri like this:

    var uri = 'mongodb://###?readPreference=nearest'; mongoose.connect(uri, opts);

    In mongoose 4.3.4 above take effect.

提交回复
热议问题