Skip to content

Commit 589d151

Browse files
rweinbergerkiku-jw
authored andcommitted
fix(cursor): cursor.count not respecting parent readPreference
Fix wrong placement and typo in options argument in cursor.s.topology.command, and test that specified readPreference is passed in to that call. Fixes NODE-1511
1 parent e567b5c commit 589d151

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

lib/mongo_client.js

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ const validOptions = require('./operations/mongo_client_ops').validOptions;
115115
*/
116116
function MongoClient(url, options) {
117117
if (!(this instanceof MongoClient)) return new MongoClient(url, options);
118-
119118
// Set up event emitter
120119
EventEmitter.call(this);
121120

lib/operations/cursor_ops.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ function count(cursor, applySkipLimit, opts, callback) {
6060
cursor.s.topology.command(
6161
`${cursor.s.ns.substr(0, delimiter)}.$cmd`,
6262
command,
63+
cursor.s.options,
6364
(err, result) => {
6465
callback(err, result ? result.result.n : null);
65-
},
66-
cursor.options
66+
}
6767
);
6868
}
6969

test/functional/cursor_tests.js

+31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const setupDatabase = require('./shared').setupDatabase;
44
const fs = require('fs');
55
const expect = require('chai').expect;
66
const Long = require('bson').Long;
7+
const sinon = require('sinon');
78

89
describe('Cursor', function() {
910
before(function() {
@@ -4501,4 +4502,34 @@ describe('Cursor', function() {
45014502

45024503
testTransformStream(config, done);
45034504
});
4505+
4506+
it('should apply parent read preference to count command', function(done) {
4507+
const configuration = this.configuration;
4508+
const ReadPreference = this.configuration.require.ReadPreference;
4509+
const client = configuration.newClient(
4510+
{ w: 1, readPreference: ReadPreference.secondary },
4511+
{ poolSize: 1, auto_reconnect: false }
4512+
);
4513+
4514+
client.connect(function(err, client) {
4515+
const db = client.db(configuration.db);
4516+
let collection, cursor, spy;
4517+
const close = e => cursor.close(() => client.close(() => done(e)));
4518+
4519+
Promise.resolve()
4520+
.then(() => db.createCollection('test_count_readPreference'))
4521+
.then(() => (collection = db.collection('test_count_readPreference')))
4522+
.then(() => collection.find())
4523+
.then(_cursor => (cursor = _cursor))
4524+
.then(() => (spy = sinon.spy(cursor.s.topology, 'command')))
4525+
.then(() => cursor.count())
4526+
.then(() =>
4527+
expect(spy.firstCall.args[2])
4528+
.to.have.nested.property('readPreference.mode')
4529+
.that.equals('secondary')
4530+
)
4531+
.then(() => close())
4532+
.catch(e => close(e));
4533+
});
4534+
});
45044535
});

0 commit comments

Comments
 (0)