diff --git a/lib/mongo_client.js b/lib/mongo_client.js index 8e4e9ca0c06..2e369817dc2 100644 --- a/lib/mongo_client.js +++ b/lib/mongo_client.js @@ -115,7 +115,6 @@ const validOptions = require('./operations/mongo_client_ops').validOptions; */ function MongoClient(url, options) { if (!(this instanceof MongoClient)) return new MongoClient(url, options); - // Set up event emitter EventEmitter.call(this); diff --git a/lib/operations/cursor_ops.js b/lib/operations/cursor_ops.js index 83d8c99ea8f..dfc51f8817a 100644 --- a/lib/operations/cursor_ops.js +++ b/lib/operations/cursor_ops.js @@ -60,10 +60,10 @@ function count(cursor, applySkipLimit, opts, callback) { cursor.s.topology.command( `${cursor.s.ns.substr(0, delimiter)}.$cmd`, command, + cursor.s.options, (err, result) => { callback(err, result ? result.result.n : null); - }, - cursor.options + } ); } diff --git a/test/functional/cursor_tests.js b/test/functional/cursor_tests.js index a2df82ce361..69bf67c7527 100644 --- a/test/functional/cursor_tests.js +++ b/test/functional/cursor_tests.js @@ -4,6 +4,7 @@ const setupDatabase = require('./shared').setupDatabase; const fs = require('fs'); const expect = require('chai').expect; const Long = require('bson').Long; +const sinon = require('sinon'); describe('Cursor', function() { before(function() { @@ -4501,4 +4502,34 @@ describe('Cursor', function() { testTransformStream(config, done); }); + + it('should apply parent read preference to count command', function(done) { + const configuration = this.configuration; + const ReadPreference = this.configuration.require.ReadPreference; + const client = configuration.newClient( + { w: 1, readPreference: ReadPreference.secondary }, + { poolSize: 1, auto_reconnect: false } + ); + + client.connect(function(err, client) { + const db = client.db(configuration.db); + let collection, cursor, spy; + const close = e => cursor.close(() => client.close(() => done(e))); + + Promise.resolve() + .then(() => db.createCollection('test_count_readPreference')) + .then(() => (collection = db.collection('test_count_readPreference'))) + .then(() => collection.find()) + .then(_cursor => (cursor = _cursor)) + .then(() => (spy = sinon.spy(cursor.s.topology, 'command'))) + .then(() => cursor.count()) + .then(() => + expect(spy.firstCall.args[2]) + .to.have.nested.property('readPreference.mode') + .that.equals('secondary') + ) + .then(() => close()) + .catch(e => close(e)); + }); + }); });