MongoError: not master and slaveOk=false

Probably anyone running Meteor and thus MongoDb in Production will sooner or later run against this problem when the master node becomes unhealthy and MongoDb selects a new Master from one of the two slave node.

This specific error will show up for a couple of minutes for us and I found the following information on the internet:

Proposed solutions:

Use in the following way:

const client = new MongoClient(mongoURL + "?readPreference=primaryPreferred", { useUnifiedTopology: true, useNewUrlParser: true });

However, as the connection happens “under the hood” of Meteor I’m struggling to understand where I can change the readPreference.

How have others tackled this problem?

1 Like