Difference between setSlaveOK() and slaveOK()

I want to know the difference between setSlaveOK() and slaveOK().

The results of slaveOK() and getMongo().setSlaveOK() were the same, but the results of slaveOK() and only setSlaveOK() were found to be different.

I want to know the function of setSlaveOK().

I also know that setSlaveOk() has been changed to setSecondaryOk() in 4.4.1 ver.

Hello @Kim_Hakseon,

In a replica set, you are connected to one of the secondary nodes with mongo shell. If you run command like show dbs there will be an error: "errmsg" : "not master and slaveOk=false". You can run read commands only after telling MongoDB so. The command to enable read operations on replica set’s secondary node is:

rs.slaveOk()

Alternatively, you can use the command:

db.getMongo().setSlaveOk()

Both the commands are the same (you can type rs.slaveOk when connected to a secondary node and see):

replset:SECONDARY> rs.slaveOk
function(value) {
    return db.getMongo().setSlaveOk(value);
}


About the db.setSlaveOk():

The functionality of this command is limited to data within a database only, I see. For example:

replset:SECONDARY> show dbs
"errmsg" : "not master and slaveOk=false"

replset:SECONDARY> use testdb 	// this is an existing database with a collection testColl
replset:SECONDARY> show collections
testColl
replset:SECONDARY> db.testColl.find()
{ "_id" : ObjectId("5f630147076a85deff34973a") }

NOTE: There is no rs.setSlaveOk() command.

3 Likes

Wow!.. Your answer surprised me.
I’m so impressed that I want to shout “Eureka.”

Thank you very much. :smiley:

Thanks. But, why did my answer surprise you?

Knowing something new always made me so interesting that I meant I was surprised to know something new.

Thank you :smiley: :smiley:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.