我正在尝试对副本集执行特定查询...但没有成功。
使用猫鼬创建连接并在副本集上执行查询的最简单方法是什么?
我的代码 1:
var mongoose = require('mongoose');
mongoose.connect('mongodb://user:pass@host:port/db,user:pass@host2:port/db');
var someSchema = new mongoose.Schema({...}, { read: 'secondary' });
var some = mongoose.model('Some', someSchema);
some.find(...) -> err [Error: not authorized for query on bd.collection]
我的代码 2:
mongoose.connect('mongodb://user:pass@host:port/db,user:pass@host2:port/db?slaveOk=true');
some.find(...) -> err [Error: not authorized for query on bd.collection]
我的代码 3:
mongoose.connect('mongodb://user:pass@host:port/db,mongodb://user:pass@host2:port/db?slaveOk=true');
some.find(...) -> err [Error: not authorized for query on bd.collection]
我的代码 4:
mongoose.connect('mongodb://user:pass@end:port/db,mongodb://user:pass@end2:port/db);
some.find(...) -> err [Error: not authorized for query on bd.collection]
我的代码 5:
mongoose.connect('mongodb://user:pass@end:port/db,mongodb://user:pass@end2:port/db?slaveOk=true);
var someSchema = new mongoose.Schema({...});
var some = mongoose.model('Some', someSchema);
some.find(...).slaveOk() -> query executed on master (apparently)
some.find(...).read('secondary') -> query executed on master (apparently - no change on mongostat of replicaset. I'm mongoHQ user and I don't trust in mongostat from dashboard.)
所以,如果我连接 mongo hostReplica:port/db -u user -p pass,并执行 db.getMongo().setSlaveOk(),我可以毫无问题地执行 db.collection.find。
我做错了什么?
谢谢