Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit 130b9a9

Browse files
authored
COMPASS 171: Fix doc & index stats on secondary reported incorrectly (#52)
Better fixes incoming in COMPASS-368 and later tickets.
1 parent 02e8d7f commit 130b9a9

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/fetch.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ var _ = require('lodash');
88
var async = require('async');
99
var mongodbNS = require('mongodb-ns');
1010
var isNotAuthorizedError = require('mongodb-js-errors').isNotAuthorized;
11+
var ReadPreference = require('mongodb').ReadPreference;
1112

1213
var 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) {
2834
function 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) {
8692
function 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 {}.');

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"ampersand-rest-collection": "^5.0.0",
2929
"async": "^1.5.2",
3030
"lodash": "^4.8.2",
31+
"mongodb": "^2.2.11",
3132
"mongodb-js-errors": "^0.2.1",
3233
"mongodb-ns": "^1.0.3",
3334
"triejs": "github:rueckstiess/triejs"

0 commit comments

Comments
 (0)