GreyCRD,
If you don’t specify a value for readPreference, the default is for applications to attempt to connect to the replica set Primary 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 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 - all inserts/deletes/updates must be executed against the primary node.
Note that there are five different readPreferences. I think that 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) will only connect to a Primary if there are no available Secondaries.
III
William Byrne III