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 –

cfg=rs.conf()
cfg.members[0].priority=100 
cfg.members[1].priority=10
cfg.members[2].priority=10
cfg.members[3].priority=0
cfg.members[4].priority=0
rs.reconfig(cfg)

What I would need is something like,

cfg=rs.conf()
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 '10.12.13.14'].priority=0
cfg.members[where name like '10.12.13.14'].priority=0
rs.reconfig(cfg)

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 == '10.12.13.14')
      member.priority = 0;
})
rs.reconfig(cfg)

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