question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting readPreference at schema level no longer works in 3.8.4

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
hyadescommented, Jul 27, 2017

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);
          });
});
0reactions
hyadescommented, Aug 2, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Read Preference — MongoDB Manual
Configure Read Preference ​​ For an example, see connection string. For a given read preference, the MongoDB drivers use the same member selection...
Read more >
mongoose/History.md - UNPKG
101, * fix(query): allow setting `writeConcern` schema option to work around MongoDB driver's `writeConcern` deprecation warning #10083 #10009 ...
Read more >
History.md - andrew.cmu.ed
addListener #2867 * fixed; call non-hook functions in schema queue #2856 * fixed; ... option #1809 [owenallenaz](https://github.com/owenallenaz) 3.8.4 ...
Read more >
api/node_modules/mongoose/History.md - GitLab
4.9.6 / 2017-04-23. fix: update parentArray references when directly assigning document arrays #5192 jhob; docs: improve schematype validator ...
Read more >
Mongoose: Read on ReplicaSet - node.js - Stack Overflow
If you want to read from a secondary, you should set your read preference to either of: secondaryPreferred - In most situations, operations...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found