2

rs.slaveOk() enables read operation on the slave member in a MongoDB replica set.

What function is used to disable it?

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Fabrice Chapuis
  • 448
  • 1
  • 5
  • 19

1 Answers1

2

Simply type rs.slaveOk(false).

In the shell you can see what is the code that is executed for each command, so if you tipe rs.slaveOk (without parenthesis) what you get is the following:

rs.slaveOk
function (value) {
    return db.getMongo().setSlaveOk(value);
}

So slaveOk is actually a function that accepts the boolean parameter and recalls setSlaveOk, which is composed by the following code:

db.getMongo().setSlaveOk
function (value) {
    if (value == undefined)
        value = true;
    this.slaveOk = value;
}

As you see, setSlaveOk has true as default value, so by specifying false you can prevent reading from the secondary.

EDIT: as correclty stated by dAm2K in the comment below, rs.slaveOk has been deprecated from version 4.4 of MongoDB; just use rs.secondaryOk.

Marco
  • 700
  • 1
  • 14
  • 26