Mongoose: Read on ReplicaSet

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

    This is the proper instantiation in Mongoose v5.9.5:

    const opts = {
        readPreference: 'nearest',
    }
    mongoose.connect(MONGODB_CONNECTION, opts)
    

    These are the different string values depending on the preference type you're looking for:

    ReadPreference.PRIMARY = 'primary';
    ReadPreference.PRIMARY_PREFERRED = 'primaryPreferred';
    ReadPreference.SECONDARY = 'secondary';
    ReadPreference.SECONDARY_PREFERRED = 'secondaryPreferred';
    ReadPreference.NEAREST = 'nearest'
    

提交回复
热议问题