How do I make slaveOk() permanent?

Today I can open a connection to a mongodb secondary like this:

PS C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe
rs0:SECONDARY> db.collection.find()
Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 }

If I run the rs.slaveOk(), then my queries work.

If I close the session, and start a new one, my queries don’t work again.

How can I make the change of slaveOk() permanent, so any new connection made to a slave is allowed to read?

:wave: Hi @MACKENZIE_CLARK, you can add rs.slaveOk() to your .mongorc.js file and that should do what you’re trying to do.

On Linux/Mac the file is in your home directory. I’m not sure where it is on Windows if you use that platform, but a quick search should return it.

3 Likes

Thanks @Doug_Duncan!

Slightly different question: is the .mongorc.js respected by programming language drivers? e.g. the C# driver.

No, mongorc.js is just for the mongo shell.

1 Like

You would set your readPreference on your connection to either secondary or secondaryPreferred to get secondary reads.

For C# the Read Preference Class page might be of help.

1 Like