Error 10107: NotWritablePrimary


I have this mongoDB connection in NodeJS


This is configured to autoscale

But I often get this error

code: 10107, 
codeName: NotWritablePrimary, 
message: not master, 
name: MongoError,

After searching I understood that it tries to write/read data to/from the secondary instance which triggers this error so how can I fix this? Here’s my connection (database = mongodb+srv://

.connect(database, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
.then((_con) => {
console.log('DB connection Successfully!');
.catch((error) => {
console.log('DB connection Failll!', error);

Thanks in advance.

Do I need to ad ReadPreference=primary&Ena​​bleReplicaSet=1 to the DSN to be mongodb+srv://​​bleReplicaSet=1 like this to make it write to/read from the primary only? Is it going to prevent triggering the issue and keep enhancing the performance through the autoscale or there\s another fix?

I’m getting another error

code: 13435, 
codeName: NotPrimaryNoSecondaryOk, 
message: not master and slaveOk=false, 
name: MongoError, 

As I understood it ties to read from secondary instances and Mongo prevents that because primary has the most recent writes but secondaries don’t so it stops reading from secondaries. If I add readPreference=primary to the connection DSN, is it going to stop triggering this error and allow reading from primary only?

Hi @Yasser_Sharaf welcome to the community!

It’s been a while since you posted this issue. Are you still having this issue currently?

If you are still seeing this:

  • Since you’re using Mongoose, have you tried using the latest version of MongoDB official node driver and see the same problem?
  • Is your Mongoose using the latest version? Mongoose is at version 6.0.7 currently. If not the latest version, you might want to upgrade and see if the issue persists.
  • Since you’re using a paid tier, have you contacted Atlas support about this issue?
  • Is the app hosted on a typical platform (e.g. a Linux server), or is it using e.g. AWS Lambda?

Normally the official MongoDB drivers will connect to the primary and do all reads from it. The replica set information is contained within the mongodb+srv protocol, so you shouldn’t need to tell the driver about the replica set connection.

Best regards