mongoose: Setting readPreference at schema level no longer works in 3.8.4

We’ve been using mongoose 3.6.x for many months, and we’ve got one model that we set a secondaryPreferred read preference on. We run a 3-member replica set, and it makes sense to offload queries for this one particular collection to our secondaries, to lessen the load on the primary.

We set it like this, basically speaking:

var TheSchema = new mongoose.Schema({
  someProp: String,
  otherProp: Number
}, {readPreference: 'secondaryPreferred'});

This has been running fine for over three months - queries were being properly routed to the secondaries, and everything was great.

After upgrading to mongoose 3.8.4, however, none of these queries work, and all of them return with this:

MongoError: not master and slaveOk=false
    at Object.toError (/Users/avianflu/dev/blend-clean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:110:11)
    at /Users/avianflu/dev/blend-clean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:685:54
    at Cursor.close (/Users/avianflu/dev/blend-clean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:959:5)
    at commandHandler (/Users/avianflu/dev/blend-clean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:685:21)
    at /Users/avianflu/dev/blend-clean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1806:9
    at Server.Base._callHandler (/Users/avianflu/dev/blend-clean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:442:41)
    at /Users/avianflu/dev/blend-clean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:485:18
    at MongoReply.parseBody (/Users/avianflu/dev/blend-clean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
    at null.<anonymous> (/Users/avianflu/dev/blend-clean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:443:20)
    at EventEmitter.emit (events.js:95:17)

Any thoughts? I’ve isolated this to the mongoose version upgrade. Server configuration is not relevant.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 20

Commits related to this issue

Most upvoted comments

With aggregate queries the read parameter is not honored. Any way to apply it on aggregation queries as well?

        it('and sends it though the driver', function(done) {
          var db = start();
          var options = {read: 'secondary', safe: {w: 'majority'}};
          var schema = new Schema({name: String}, options);
          var M = db.model(random(), schema);
          var q = M.aggregate([{$match: {day: 1}}]);

          // stub the internal query options call
          var getopts = q._optionsForExec;
          q._optionsForExec = function(model) {
            q._optionsForExec = getopts;

            var ret = getopts.call(this, model);

            assert.ok(ret.readPreference);
            assert.equal(ret.readPreference.mode, 'secondary');
            assert.deepEqual({w: 'majority'}, ret.safe);
            called = true;

            return ret;
          };

          q.exec(function(err) {
            if (err) {
              return done(err);
            }
            assert.ok(called);
            db.close(done);
          });
});