Update priorities on mongo based on host name using JS

All we need is an easy explanation of the problem, so here it is.

Is there a way to adjust priorities of the nodes based on hostname instead of sequence.

Using sequence –


What I would need is something like,

cfg.members[where name like 'abc'].priority=100 
cfg.members[where name like 'pqr'].priority=10
cfg.members[where name like 'xyz'].priority=10
cfg.members[where name like ''].priority=0
cfg.members[where name like ''].priority=0

Updating bulk of replica sets of different clusters would be difficulty with sequences.

Sequence of hostnames are not in ordered.
So if I need to update specific host with highest priority during maintenance I cannot rely on sequence.
May be using java script is easy.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Yes, you can use a Javascript like this:

cfg = rs.conf()
cfg.members.forEach(function(member) {
   if (member.host.includes("abc"))
      member.priority = 100
   else if (/xyz/.test(member.host))
      member.priority = 10
   else if (member.host == '')
      member.priority = 0;

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply