@@ -8,9 +8,15 @@ var _ = require('lodash');
88var async = require ( 'async' ) ;
99var mongodbNS = require ( 'mongodb-ns' ) ;
1010var isNotAuthorizedError = require ( 'mongodb-js-errors' ) . isNotAuthorized ;
11+ var ReadPreference = require ( 'mongodb' ) . ReadPreference ;
1112
1213var debug = require ( 'debug' ) ( 'mongodb-index-model:fetch' ) ;
1314
15+ /**
16+ * The default read preference.
17+ */
18+ var READ = ReadPreference . PRIMARY_PREFERRED ;
19+
1420/**
1521 * helper function to attach objects to the async.auto task structure.
1622 * @param {any } anything pass in any variable to attach it to the name
@@ -28,7 +34,7 @@ function attach(anything, done) {
2834function getIndexes ( done , results ) {
2935 var db = results . db ;
3036 var ns = mongodbNS ( results . namespace ) ;
31- db . db ( ns . database ) . collection ( ns . collection ) . indexes ( function ( err , indexes ) {
37+ db . db ( ns . database , { readPreference : { mode : READ } } ) . collection ( ns . collection ) . indexes ( function ( err , indexes ) {
3238 if ( err ) {
3339 done ( err ) ;
3440 }
@@ -52,7 +58,7 @@ function getIndexStats(done, results) {
5258 { $indexStats : { } } ,
5359 { $project : { name : 1 , usageHost : '$host' , usageCount : '$accesses.ops' , usageSince : '$accesses.since' } }
5460 ] ;
55- var collection = db . db ( ns . database ) . collection ( ns . collection ) ;
61+ var collection = db . db ( ns . database , { readPreference : { mode : READ } } ) . collection ( ns . collection ) ;
5662 collection . aggregate ( pipeline , function ( err , res ) {
5763 if ( err ) {
5864 if ( isNotAuthorizedError ( err ) ) {
@@ -86,7 +92,7 @@ function getIndexStats(done, results) {
8692function getIndexSizes ( done , results ) {
8793 var db = results . db ;
8894 var ns = mongodbNS ( results . namespace ) ;
89- db . db ( ns . database ) . collection ( ns . collection ) . stats ( function ( err , res ) {
95+ db . db ( ns . database , { readPreference : { mode : READ } } ) . collection ( ns . collection ) . stats ( function ( err , res ) {
9096 if ( err ) {
9197 if ( isNotAuthorizedError ( err ) ) {
9298 debug ( 'Not authorized to get collection stats. Returning default for indexSizes {}.' ) ;
0 commit comments