I'm trying to execute a specific query on replica set... but without success.
What is the simplest way to create a connection and execute a query on replicaset with mongoose?
My code-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]
My code-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]
My code-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]
My code-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]
My code-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.)
SO, if I connect with mongo hostReplica:port/db -u user -p pass, and exec db.getMongo().setSlaveOk(), I can exec db.collection.find without problem.
What I'm doing wrong?
Thanks
I followed this doc and it works fine for me. My connection string for Mongoose looks like this:
mongodb://user:pass@host1,user:pass@host2/db?replicaSet=myReplica&readPreference=secondaryPreferred
I think that slaveOk is deprecated, and we should now use the readPreference option.
As I use Nodejs/Mongoose for a read-only api, I want to read from the slave(s), and keep the master for insertions mainly (which are done outside of Nodejs). That's why I use secondaryPreferred.