@@ -20,6 +20,13 @@ const {
20
20
21
21
const debug = require ( 'debug' ) ( 'mongodb-data-service:instance-detail-helper' ) ;
22
22
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
+
23
30
/**
24
31
* aggregates stats across all found databases
25
32
* @param {Object } results async.auto results
@@ -78,7 +85,7 @@ function getBuildInfo(results, done) {
78
85
} ;
79
86
80
87
const adminDb = db . databaseName === 'admin' ? db : db . admin ( ) ;
81
- adminDb . command ( spec , { } , function ( err , res ) {
88
+ adminDb . command ( spec , getReadPreferenceOptions ( db ) , function ( err , res ) {
82
89
if ( err ) {
83
90
// buildInfo doesn't require any privileges to run, so if it fails,
84
91
// something really went wrong and we should return the error.
@@ -98,7 +105,7 @@ function getCmdLineOpts(results, done) {
98
105
} ;
99
106
100
107
const adminDb = db . databaseName === 'admin' ? db : db . admin ( ) ;
101
- adminDb . command ( spec , { } , function ( err , res ) {
108
+ adminDb . command ( spec , getReadPreferenceOptions ( db ) , function ( err , res ) {
102
109
if ( err ) {
103
110
debug ( 'getCmdLineOpts failed!' , err ) ;
104
111
return done ( null , { errmsg : err . message } ) ;
@@ -213,7 +220,7 @@ function getHostInfo(results, done) {
213
220
} ;
214
221
215
222
const adminDb = db . databaseName === 'admin' ? db : db . admin ( ) ;
216
- adminDb . command ( spec , { } , function ( err , res ) {
223
+ adminDb . command ( spec , getReadPreferenceOptions ( db ) , function ( err , res ) {
217
224
if ( err ) {
218
225
if ( isNotAuthorized ( err ) ) {
219
226
// if the error is that the user is not authorized, silently ignore it
@@ -253,12 +260,14 @@ function listDatabases(results, done) {
253
260
listDatabases : 1
254
261
} ;
255
262
263
+ const options = getReadPreferenceOptions ( db ) ;
264
+
256
265
const adminDb = db . databaseName === 'admin' ? db : db . admin ( ) ;
257
- adminDb . command ( spec , { } , function ( err , res ) {
266
+ adminDb . command ( spec , options , function ( err , res ) {
258
267
if ( err ) {
259
268
if ( isNotAuthorized ( err ) ) {
260
269
// 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 ) {
262
271
if ( err ) {
263
272
done ( err ) ;
264
273
return ;
@@ -343,7 +352,7 @@ function getDatabase(client, db, name, done) {
343
352
dbStats : 1
344
353
} ;
345
354
346
- const options = { } ;
355
+ const options = getReadPreferenceOptions ( db ) ;
347
356
debug ( 'running dbStats for `%s`...' , name ) ;
348
357
client . db ( name ) . command ( spec , options , function ( err , res ) {
349
358
if ( err ) {
@@ -376,8 +385,15 @@ function getDatabases(results, done) {
376
385
function getUserInfo ( results , done ) {
377
386
const db = results . db ;
378
387
388
+ const options = getReadPreferenceOptions ( db ) ;
389
+
379
390
// 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 (
381
397
err ,
382
398
res
383
399
) {
@@ -394,7 +410,7 @@ function getUserInfo(results, done) {
394
410
}
395
411
const user = res . authInfo . authenticatedUsers [ 0 ] ;
396
412
397
- db . command ( { usersInfo : user , showPrivileges : true } , { } , function (
413
+ db . command ( { usersInfo : user , showPrivileges : true } , options , function (
398
414
_err ,
399
415
_res
400
416
) {
@@ -491,15 +507,7 @@ function getDatabaseCollections(db, done) {
491
507
492
508
const spec = { } ;
493
509
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 ) {
503
511
if ( err ) {
504
512
if ( isNotAuthorized ( err ) || isMongosLocalException ( err ) ) {
505
513
// if the error is that the user is not authorized, silently ignore it
0 commit comments