How to prevent secondary node of mongodb to become primary node is very easy by setting the priority of it , priority variable in mongodb has own priority while election to become primary. You can set priority in replication of mongoDB, this is basically used to prevent secondary to become primary. Setting of this is mongoDB replication environment variable is very easy process in mongoDB, following are the steps;
Step 1. Copy the replica set configuration to a variable
In the mongo shell, use rs.conf() to retrieve the replica set configuration and assign it to a variable. For example:
cfg = rs.conf()
Step 2. Change each member’s priority value.
Change each member’s members[n].priority value, as configured in the members array. first of all check position of node which you want to change the priority.
cfg.members.priority = 0.5 cfg.members.priority = 3 cfg.members.priority = 2
Step 3. Assign the replica set the new configuration.
rs.reconfig() to apply the new configuration.
rs.reconfig(cfg) or rs.reconfig(cfg, force: true);
This operation updates the configuration of the replica set using the configuration defined by the value of cfg.