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.