猫鼬:在ReplicaSet上阅读 [英] Mongoose: Read on ReplicaSet
问题描述
我有一个mongodb副本集,我想从该副本集中读取主数据库和辅助数据库的数据.
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.connect('mongodb://user:password@54.230.1.1,user:password@54.230.1.2,user:password@54.230.1.3/PanPanDB?replicaSet=rs0&readPreference=nearest');
它不起作用..我的应用程序继续从主数据库读取..有任何建议吗?
It doesn't work.. My application continues to read from the primary.. Any suggestion please?
推荐答案
按照猫鼬网站,您可以添加以下说明以在猫鼬上配置ReadPreference:
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);
这已经使用Mongoose版本3.8.9进行了测试
This has been tested using Mongoose version 3.8.9
这篇关于猫鼬:在ReplicaSet上阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!