Have a requirement to always connect & query (find / delete operations) against the PRIMARY MongoDB in a replica set.
Hi Richard,
The isMaster() command is normally used to find out whether or not an instance is the primary of the replica set. Not to set a client to read from the primary of a replica set.
The connection preference to perform queries (i.e. find() ) can be configured by setting Read Preference. However, write/update operations (i.e. deleteOne()), can only be performed on the replica set primary member. Also see Replication for more information.
How can I achieve the same using the Node Mongo DB Driver - is it just a case of adding the above as options to the Connection ?
See MongoDB Node.JS Driver: Connect to a Replica Set for code example and information. Note that the default read preference value of the driver is to read from primary.
You may find the following resources useful:
If you have further questions, could you provide the following:
Regards,
Wan.