0

I have a mongoose find() query where I'd like to then use .populate to fill in some of the cross references. I'm running this against the secondary node of a mongo replica set. I can run the query just fine against the primary, and I can run "normal" find and aggregate queries (without population) against the secondary using the documented read preferences.

I get stuck when adding the .populate() line. I get the following error:

{ [MongoError: not master and slaveOk=false] name: 'MongoError' }

The appropriate schemas are defined, etc. and the query, with the .populate line, runs fine against the primary. Just not against the secondary (though again, the same query without populate() line runs correctly against the secondary). Any ideas? Thanks!

  • What is your readPreference and where are you setting it? Is it set globally or are you just using the `.read()` modifiers on queries? – Neil Lunn Jan 10 '15 at 00:42
  • I've tried both. On the connection URI with "?replicaSet=xxxx&readPreference=secondary" and in the Model.find() query with .read('secondary'). I also passed in a {replset: {xxx}} options doc as the second parameter of the mongoose.connect(dburi, options) call. I've tried each independently and together. – Thomas Marshall Jan 12 '15 at 17:26
  • Can you log the population query sent to MongoDB and include it in the question? The problem is that the read preference isn't set correctly so we need to track down where it is set (or find out it isn't set). I couldn't find any related issues in the mongoose project and I doubt this hasn't come up before, so I think there must be an error or omission somewhere in your code. – wdberkeley Jan 16 '15 at 17:41
  • `db.connect = function(dbConfig) { dbURI = "mongodb://" + dbConfig.user + ":" + dbConfig.pass + "@" + dbConfig.host + ":" + dbConfig.port + "/" + dbConfig.name + "?replicaSet=FitRS&readPreference=secondary"; var options = { db: {readPreference: 'ReadPreference.SECONDARY'}, replset: { rs_name: 'xxxxx', connectWithNoPrimary: true, readPreference: 'ReadPreference.SECONDARY'}}; mongoose.connect(dbURI, options); }` – Thomas Marshall Jan 19 '15 at 15:41
  • `var query = hndl.find({state: 3}).read('secondary') .where(..).select({...}).populate({path: 'xref schema', select: 'field1 field2 field3'})..exec(function (err, res) {});` – Thomas Marshall Jan 19 '15 at 15:42

1 Answers1

0

You can use populate().read('secondaryPrefered') this will allow reading data from secondary replica sets.

Rony Cherian
  • 114
  • 3