diff options
author | Adam Midvidy <amidvidy@gmail.com> | 2016-01-04 16:32:30 -0500 |
---|---|---|
committer | Spencer T Brody <spencer@mongodb.com> | 2016-04-19 10:58:37 -0400 |
commit | a43b64811a0ce3edc798c18612a91e2460a6414c (patch) | |
tree | 915cbccf46c65f159eee089d78be6460ee880f3d | |
parent | 07981debd3335d53909e169baad92240916baf53 (diff) | |
download | mongo-a43b64811a0ce3edc798c18612a91e2460a6414c.tar.gz |
SERVER-22043 attach read preference in count helper
(cherry picked from commit a7820c6af9c4474239f8f80c88d9de7722fa900a)
-rw-r--r-- | jstests/noPassthrough/count_helper_read_preference.js | 40 | ||||
-rw-r--r-- | src/mongo/shell/query.js | 2 |
2 files changed, 41 insertions, 1 deletions
diff --git a/jstests/noPassthrough/count_helper_read_preference.js b/jstests/noPassthrough/count_helper_read_preference.js new file mode 100644 index 00000000000..b888107ccd6 --- /dev/null +++ b/jstests/noPassthrough/count_helper_read_preference.js @@ -0,0 +1,40 @@ +// Tests that the read preference set on the connection is used when we call the count helper. +(function() { + "use strict"; + + var commandsRan = []; + + // Create a new DB object backed by a mock connection. + function MockMongo() {}; + MockMongo.prototype = Mongo.prototype; + MockMongo.prototype.runCommand = function(db, cmd, opts) { + commandsRan.push({db: db, cmd: cmd, opts:opts}); + return {ok: 1, n: 100}; + }; + var db = new DB(new MockMongo(), "test"); + + assert.eq(commandsRan.length, 0); + + // Run a count with no readPref. + db.getMongo().setReadPref(null); + db.foo.count(); + + // Check that there is no readPref on the command document. + assert.eq(commandsRan.length, 1); + assert.docEq(commandsRan[0].cmd, {count: "foo", fields: {}, query: {}}); + + commandsRan = []; + + // Run with readPref secondary. + db.getMongo().setReadPref("secondary"); + db.foo.count(); + + // Check that we have wrapped the command and attached the read preference. + assert.eq(commandsRan.length, 1); + assert.docEq(commandsRan[0].cmd, + {query: {count: "foo", + fields: {}, + query: {}}, + $readPreference: {mode: "secondary"}}); + +})(); diff --git a/src/mongo/shell/query.js b/src/mongo/shell/query.js index 1dd6d7fff1d..f0067fe4837 100644 --- a/src/mongo/shell/query.js +++ b/src/mongo/shell/query.js @@ -364,7 +364,7 @@ DBQuery.prototype._convertToCountCmd = function(applySkipLimit) { DBQuery.prototype.count = function(applySkipLimit) { var cmd = this._convertToCountCmd(applySkipLimit); - var res = this._db.runCommand(cmd); + var res = this._db.runReadCommand(cmd); if (res && res.n != null) return res.n; throw _getErrorWithCode(res, "count failed: " + tojson(res)); |