@@ -20,6 +20,13 @@ const {
2020
2121const debug = require ( 'debug' ) ( 'mongodb-data-service:instance-detail-helper' ) ;
2222
23+ function getReadPreferenceOptions ( db ) {
24+ // `db.command` does not use the read preference set on the
25+ // connection, so here we explicitly to specify it in the options.
26+ const readPreference = db . s ? db . s . readPreference : ReadPreference . PRIMARY ;
27+ return { readPreference } ;
28+ }
29+
2330/**
2431 * aggregates stats across all found databases
2532 * @param {Object } results async.auto results
@@ -78,7 +85,7 @@ function getBuildInfo(results, done) {
7885 } ;
7986
8087 const adminDb = db . databaseName === 'admin' ? db : db . admin ( ) ;
81- adminDb . command ( spec , { } , function ( err , res ) {
88+ adminDb . command ( spec , getReadPreferenceOptions ( db ) , function ( err , res ) {
8289 if ( err ) {
8390 // buildInfo doesn't require any privileges to run, so if it fails,
8491 // something really went wrong and we should return the error.
@@ -98,7 +105,7 @@ function getCmdLineOpts(results, done) {
98105 } ;
99106
100107 const adminDb = db . databaseName === 'admin' ? db : db . admin ( ) ;
101- adminDb . command ( spec , { } , function ( err , res ) {
108+ adminDb . command ( spec , getReadPreferenceOptions ( db ) , function ( err , res ) {
102109 if ( err ) {
103110 debug ( 'getCmdLineOpts failed!' , err ) ;
104111 return done ( null , { errmsg : err . message } ) ;
@@ -213,7 +220,7 @@ function getHostInfo(results, done) {
213220 } ;
214221
215222 const adminDb = db . databaseName === 'admin' ? db : db . admin ( ) ;
216- adminDb . command ( spec , { } , function ( err , res ) {
223+ adminDb . command ( spec , getReadPreferenceOptions ( db ) , function ( err , res ) {
217224 if ( err ) {
218225 if ( isNotAuthorized ( err ) ) {
219226 // if the error is that the user is not authorized, silently ignore it
@@ -253,12 +260,14 @@ function listDatabases(results, done) {
253260 listDatabases : 1
254261 } ;
255262
263+ const options = getReadPreferenceOptions ( db ) ;
264+
256265 const adminDb = db . databaseName === 'admin' ? db : db . admin ( ) ;
257- adminDb . command ( spec , { } , function ( err , res ) {
266+ adminDb . command ( spec , options , function ( err , res ) {
258267 if ( err ) {
259268 if ( isNotAuthorized ( err ) ) {
260269 // eslint-disable-next-line no-shadow
261- adminDb . command ( { connectionStatus : 1 , showPrivileges : 1 } , { } , function ( err , res ) {
270+ adminDb . command ( { connectionStatus : 1 , showPrivileges : 1 } , options , function ( err , res ) {
262271 if ( err ) {
263272 done ( err ) ;
264273 return ;
@@ -343,7 +352,7 @@ function getDatabase(client, db, name, done) {
343352 dbStats : 1
344353 } ;
345354
346- const options = { } ;
355+ const options = getReadPreferenceOptions ( db ) ;
347356 debug ( 'running dbStats for `%s`...' , name ) ;
348357 client . db ( name ) . command ( spec , options , function ( err , res ) {
349358 if ( err ) {
@@ -376,8 +385,15 @@ function getDatabases(results, done) {
376385function getUserInfo ( results , done ) {
377386 const db = results . db ;
378387
388+ const options = getReadPreferenceOptions ( db ) ;
389+
379390 // get the user privileges
380- db . command ( { connectionStatus : 1 , showPrivileges : true } , { } , function (
391+ db . command ( {
392+ connectionStatus : 1 ,
393+ showPrivileges : true
394+ } ,
395+ options ,
396+ function (
381397 err ,
382398 res
383399 ) {
@@ -394,7 +410,7 @@ function getUserInfo(results, done) {
394410 }
395411 const user = res . authInfo . authenticatedUsers [ 0 ] ;
396412
397- db . command ( { usersInfo : user , showPrivileges : true } , { } , function (
413+ db . command ( { usersInfo : user , showPrivileges : true } , options , function (
398414 _err ,
399415 _res
400416 ) {
@@ -491,15 +507,7 @@ function getDatabaseCollections(db, done) {
491507
492508 const spec = { } ;
493509
494- /**
495- * @note : Durran: For some reason the listCollections call does not take into
496- * account the read preference that was set on the db instance - it only looks
497- * in the passed options: https://github.com/mongodb/node-mongodb-native/blob/2.2/lib/db.js#L671
498- */
499- const rp = db . s ? db . s . readPreference : ReadPreference . PRIMARY ;
500- const options = { readPreference : rp } ;
501-
502- db . listCollections ( spec , options ) . toArray ( function ( err , res ) {
510+ db . listCollections ( spec , getReadPreferenceOptions ( db ) ) . toArray ( function ( err , res ) {
503511 if ( err ) {
504512 if ( isNotAuthorized ( err ) || isMongosLocalException ( err ) ) {
505513 // if the error is that the user is not authorized, silently ignore it
0 commit comments