Skip to content

Commit a24a67c

Browse files
committed
NODE-475 ensure create/ensure/drop indexes use primary, ignoring any readpreferences
1 parent cac69c2 commit a24a67c

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/collection.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,8 @@ Collection.prototype.dropIndex = function(indexName, options, callback) {
10281028
var args = Array.prototype.slice.call(arguments, 1);
10291029
callback = args.pop();
10301030
options = args.length ? args.shift() || {} : {};
1031+
// Run only against primary
1032+
options.readPreference = ReadPreference.PRIMARY;
10311033

10321034
// Delete index command
10331035
var cmd = {'deleteIndexes':this.s.name, 'index':indexName};
@@ -1139,6 +1141,7 @@ Collection.prototype.listIndexes = function(options) {
11391141
Collection.prototype.ensureIndex = function(fieldOrSpec, options, callback) {
11401142
if(typeof options == 'function') callback = options, options = {};
11411143
options = options || {};
1144+
// Execute ensureIndex
11421145
this.s.db.ensureIndex(this.s.name, fieldOrSpec, options, callback);
11431146
}
11441147

lib/db.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,10 @@ Db.prototype.createIndex = function(name, fieldOrSpec, options, callback) {
710710
options = args.length ? args.shift() || {} : {};
711711
options = typeof callback === 'function' ? options : callback;
712712
options = options == null ? {} : options;
713+
// Clone options
714+
options = shallowClone(options);
715+
// Run only against primary
716+
options.readPreference = ReadPreference.PRIMARY;
713717

714718
// Get the write concern options
715719
var finalOptions = writeConcern({}, self, options);
@@ -768,6 +772,10 @@ Db.prototype.ensureIndex = function(name, fieldOrSpec, options, callback) {
768772
var self = this;
769773
if(typeof options == 'function') callback = options, options = {};
770774
options = options || {};
775+
// Clone options
776+
options = shallowClone(options);
777+
// Run only against primary
778+
options.readPreference = ReadPreference.PRIMARY;
771779

772780
// Get the write concern options
773781
var finalOptions = writeConcern({}, self, options);

0 commit comments

Comments
 (0)