Search code examples
mongodbshardingreplicaset

How can I use HOST VARIABLE in rs.add("host:port") in mongoDB Replica Set


I am setting up MongoDB Sharded Cluster via Shell script and would like to run

$mongo --port 27027 --eval 'rs.add("$HOSTNAME:27037")'

Is there is way to add RS member via variable instead of hard-coded string?


Solution

  • I was able to add the host like this:

    RS-LAB:PRIMARY> var host = hostname()

    RS-LAB:PRIMARY> var port = 27027

    RS-LAB:PRIMARY> var hp = host+':'+port

    RS-LAB:PRIMARY> printjson(hp)

    "vxxxx.domain.com:27027"

    RS-LAB:PRIMARY> rs.add(hp) { "ok" : 1, "operationTime" : Timestamp(1530299232, 1), "$clusterTime" : { "clusterTime" : Timestamp(1530299232, 1), "signature" : { "hash" : BinData(0,"BiLMYfYVWTgS+pQeGnZK7+N6Vf8="), "keyId" : NumberLong("6572537789634576385") } } }

    RS-LAB:PRIMARY>