Mongoose: Read on ReplicaSet

后端 未结 6 1945
暖寄归人
暖寄归人 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:56

    Following the documentation found on MongoDB website and on Mongoose web site, you can add this instruction for configuring the ReadPreference on Mongoose:

    var opts = { replSet: {readPreference: 'ReadPreference.NEAREST'} };
    

    mongoose.connect('mongodb://###:###@###:###/###', opts);

    This has been tested using Mongoose version 3.8.9

提交回复
热议问题