strange mongodb and mongoose error: not master and slaveOk=false error
Asked Answered
C

6

47

I'm getting a strange error in my node.js app

not master and slaveOk=false code 13435

I'm doing a findOne query using mongoose 4.0.2 hitting a mongodb 3.0.1. The query was working earlier today.

What is this error? I'm running on mongohq on their standard per gb plan.

Casiano answered 6/5, 2015 at 22:31 Comment(1)
C
49

That means you're trying to read from a secondary node in a replica set, you can only read from the primary node by default.

You can allow a secondary node to accept reads by running rs.slaveOk() in a mongo shell that is connected to that secondary node. Allowing reads from a secondary is not recommended, because you could be reading stale data if the node isn't yet synced with the primary node.

UPDATE: As Janusz Slota's comment points out, rs.slaveOk() is no longer used. Use rs.secondaryOk() instead, however this is still not recommended. Here's the documentation for rs.secondaryOk().

Cecilius answered 6/5, 2015 at 22:54 Comment(3)
hmm so that sounds like a mongohq error since I didn't explicitly set up a replica set - and maybe they set one up on their end.Casiano
That's most likely the case. I don't know which driver you're using, but I'm pretty sure they are all designed to detect and handle connections to replica sets automatically.Cecilius
slaveOK() is no longer politically correct, use secondaryOk()Handel
W
24

I solved this by simply fixing the URI my Node.js application was using to connect to MongoDB with Mongoose.

When this error ocurred, my URI was

mongodb://user:password@host:port/datatabase,

and this was giving me the error not master and slaveOK=false.

Then I changed the URI to add the Replica Set information, and the URI became something like this:

mongodb://user:password@host:port,replicaSetHost:replicaSetPort/database?replicaSet=rs-someServer.

Now, I don't know if this is a general pattern, because this configuration is the one used by MongoLab, which is where my DataBase is hosted. However, it's likely you will solve the problem by adding the replica set information in the URI as well.

Wholly answered 7/12, 2015 at 12:9 Comment(3)
The best you saved my production app from negative reviews :)Jankey
Yep, I needed &replicaSet=rs0&w=majority.Missal
To find the value to use for the replicaSet query parameter, execute the following in a mongo shell: rs.status().setNashbar
C
6

I solved the problem(happened by connecting to a instance of mongoDB on IBM) with

mongoose.connect('mongodb://host1:port1/?replicaSet=rsName');

From: https://mongoosejs.com/docs/connections.html#replicaset_connections

Choochoo answered 26/3, 2019 at 18:0 Comment(0)
N
3

It is likely your master node failed and you are trying to read from a slave node but by default (and a safety practice such as data synchronization) your reading source is set to primary node only.

You can attach options at the end of your connection string such as ?readPreference=secondary which will use the secondary connection

Ns answered 11/2, 2016 at 22:38 Comment(0)
A
3

You can use readPreference to your connection string.

mongodb://localhost:27017/?readPreference=secondary&directConnection=true&ssl=false

when primary does not allow you to connect, you can use secondary preference.

Aylesbury answered 4/10, 2021 at 9:55 Comment(2)
This saved my lifeDukes
typo "seconday"Verniavernice
M
2

This works for me on mongoose 4.x and mongodb 3.0.x:

model.find().read('secondary').....

see http://mongoosejs.com/docs/api.html#query_Query-read

Microsecond answered 10/8, 2015 at 10:2 Comment(1)
it makes sense. A replicaset db should be use just to read so in my opinion this is the best approach to do this.Hushaby

© 2022 - 2024 — McMap. All rights reserved.