node.js - Node jsMongoDB replica set array in javascript - Stack Overflow

Warning: I'm a novice programmer (more of sysadmin).We were given an node js application that�

Warning: I'm a novice programmer (more of sysadmin). We were given an node js application that's using MongoDB. From what I can tell, the mongo.js file is using mongojs and monq java classes. It was setup with only one MongoDB and I'm trying to setup a new HA environment to use a replica set. Here is what they provided:

var mongojs = require('mongojs');
var monq = require('monq');
var dbName = 'exampledb';
var db = mongojs(dbName, ['collections']);
var client = monq('mongodb://127.0.0.1/exampledb', { w: 1 });

exports.db = db;
exports.ObjectId = mongojs.ObjectId;
exports.monqClient = client;

Now for a replica set, according to this article, I need to make the following change:

var db = mongojs('replset0, replset1, replset2/mydb?slaveOK=true?', ['collections']);

I'm not entirely sure what I need to do for the line after that. I'm guessing I would have to create an array that would contain the host name and port # for each member of the replica set (setup is primary, secondary, arbiter) such as:

var replSet = new replSet();
var replSet[0] = "server0:port0"
var replSet[1] = "server1.:port1"
var replSet[2] = "server2.:port2"

How would I go about detecting which node is the primary? Also if the primary were to fail, I would have to restart the node js application (using forever)?

Warning: I'm a novice programmer (more of sysadmin). We were given an node js application that's using MongoDB. From what I can tell, the mongo.js file is using mongojs and monq java classes. It was setup with only one MongoDB and I'm trying to setup a new HA environment to use a replica set. Here is what they provided:

var mongojs = require('mongojs');
var monq = require('monq');
var dbName = 'exampledb';
var db = mongojs(dbName, ['collections']);
var client = monq('mongodb://127.0.0.1/exampledb', { w: 1 });

exports.db = db;
exports.ObjectId = mongojs.ObjectId;
exports.monqClient = client;

Now for a replica set, according to this article, I need to make the following change:

var db = mongojs('replset0., replset1., replset2./mydb?slaveOK=true?', ['collections']);

I'm not entirely sure what I need to do for the line after that. I'm guessing I would have to create an array that would contain the host name and port # for each member of the replica set (setup is primary, secondary, arbiter) such as:

var replSet = new replSet();
var replSet[0] = "server0:port0"
var replSet[1] = "server1.:port1"
var replSet[2] = "server2.:port2"

How would I go about detecting which node is the primary? Also if the primary were to fail, I would have to restart the node js application (using forever)?

Share Improve this question edited Mar 11, 2015 at 20:25 MForMarlon 8619 silver badges24 bronze badges asked Oct 29, 2013 at 18:21 nocodenocode 1,3161 gold badge15 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I found the answer as it's calling MongoDB's URI http://docs.mongodb/manual/reference/connection-string/

Should be something like:

var client = monq('mongodb://server0:port0,server1:port1,server2:port2/[dbname]?replicaSet=[replicaSet Name]

First question: As long as you give it all of the members in the connection string, the mongojs driver should be able to figure out which one is primary. No need to figure it out yourself.

Second question: No, you don't have to restart the node app. The other members in the set will elect a new primary, although it takes time for mongo to detect failure and run the election.

For more information, see the mongodb docs on replica sets.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745492264a4630037.html

相关推荐

  • node.js - Node jsMongoDB replica set array in javascript - Stack Overflow

    Warning: I'm a novice programmer (more of sysadmin).We were given an node js application that�

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信