error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:128

When attempting to read data on a secondary, the following error is recieved:

error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:128

This happens because you are connected to a secondary and slaveOK is set to false. When connecting via  a shell, use the following to enable reads:

rs.slaveOk()

The one caveat with this is that on a system with many secondary’s, there can sometimes be latency between the master and secondary’s which may result in stale data. slaveOk works on the proviso that the user is happy reading data based on eventual consistency.

When connecting via an application, reading from a secondary is achieved via “Read preference”

Advertisements

error: { "$err" : "not master or secondary; cannot currently read from this replSet member", "code" : 13436 }

When attempting to connect to a mongo standalone server without replica set from an application server, the following error is recieved:

error: { "$err" : "not master or secondary; cannot currently read from this replSet member", "code" : 13436 }

On investigation it was found that /etc/mongo.conf had the follwoing parameter set:

replSet        = repqamongo

which means that the mongod process was started as an uninitialised replica.

To resolve, either remove the flag and restart mongod process or run rs.initiate() to turn into a single node replica (a bit pointless though)