{"id":89,"date":"2012-08-28T17:24:17","date_gmt":"2012-08-28T16:24:17","guid":{"rendered":"http:\/\/www.flamingpenguin.co.uk\/blog\/?p=89"},"modified":"2012-08-28T17:24:17","modified_gmt":"2012-08-28T16:24:17","slug":"adding-nodes-to-a-mongodb-replica-set-using-the-java-driver","status":"publish","type":"post","link":"https:\/\/www.flamingpenguin.co.uk\/blog\/2012\/08\/28\/adding-nodes-to-a-mongodb-replica-set-using-the-java-driver\/","title":{"rendered":"Adding nodes to a MongoDB replica set using the Java driver"},"content":{"rendered":"<p>MongoDB console has a method for <a href=\"https:\/\/github.com\/mongodb\/mongo\/blob\/1eb57466fc10c3538e1b1585fc709451909af359\/shell\/utils.js\">adding a replica to a replica set<\/a> called rs.add. The Java driver doesn&#8217;t support this directly, but you can use the code from the mongo shell to implement add directly by running commands against the admin database.<\/p>\n<pre lang=\"javascript\">\r\nrs.add = function (hostport, arb) {\r\n    var cfg = hostport;\r\n\r\n    var local = db.getSisterDB(\"local\");\r\n    assert(local.system.replset.count() <= 1, \"error: local.system.replset has unexpected contents\");\r\n    var c = local.system.replset.findOne();\r\n    assert(c, \"no config object retrievable from local.system.replset\");\r\n\r\n    c.version++;\r\n\r\n    var max = 0;\r\n    for (var i in c.members)\r\n        if (c.members[i]._id > max) max = c.members[i]._id;\r\n    if (isString(hostport)) {\r\n        cfg = { _id: max + 1, host: hostport };\r\n        if (arb)\r\n            cfg.arbiterOnly = true;\r\n    }\r\n    c.members.push(cfg);\r\n    return this._runCmd({ replSetReconfig: c });\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB console has a method for adding a replica to a replica set called rs.add. The Java driver doesn&#8217;t support this directly, but you can use the code from the mongo shell to implement add directly by running commands against &hellip; <a href=\"https:\/\/www.flamingpenguin.co.uk\/blog\/2012\/08\/28\/adding-nodes-to-a-mongodb-replica-set-using-the-java-driver\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":0,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}