Re: Error writin in a replicaset with nodejs

From: William Byrne III <william.byrne@xxxxxxxxxxx>
To: mongodb-user <mongodb-user@xxxxxxxxxxxxxxxx>
Date: Wed, 27 Apr 2016 21:48:39 -0700 (PDT)
Why ads?


GreyCRD,

If you don’t specify a value for readPreference 
<https://docs.mongodb.org/manual/core/read-preference/>, the default is for 
applications to attempt to connect to the replica set Primary 
<https://docs.mongodb.org/manual/core/replica-set-members/#replica-set-primary-member
only. Given you could not read documents (or even connect I suspect) until 
you added readPreference=secondaryPreferred, I believe that this replica 
set has no accessible Primary.

I suggest you connect directly using just the mongo shell and run 
rs.status() to confirm the status of all nodes, and then use the replica 
sets troubleshooting guide 
<https://docs.mongodb.org/manual/tutorial/troubleshoot-replica-sets/> to 
resolve the issues you find.

The error you see trying to write documents even after you have connected 
using readPreference=secondaryPreferred and read documents successfully 
means you are connected to a Secondary (or slave) node, not a Primary (or 
master) node. MongoDB does not support multi-master replication 
<https://en.wikipedia.org/wiki/Multi-master_replication> - all 
inserts/deletes/updates must be executed against the primary node. 

Note that there are five different readPreferences 
<https://docs.mongodb.org/manual/core/read-preference/#read-preference-modes>. 
I think that primaryPreferred 
<https://docs.mongodb.org/manual/reference/read-preference/#primaryPreferred
might suit your purposes better. It will connect to a Primary if one is 
available, or a Secondary if not. In contrast, the mode you are using (
secondaryPreferred 
<https://docs.mongodb.org/manual/reference/read-preference/#secondaryPreferred>) 
will only connect to a Primary if there are no available Secondaries.

III
------------------------------

William Byrne III


-- 
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
--- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@xxxxxxxxxxxxxxxx.
To post to this group, send email to mongodb-user@xxxxxxxxxxxxxxxx.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/e623fdb7-0900-43c9-b058-07f8f4410b27%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Why ads?