From ec6c6187512286aecb27b269b28148a11222371e Mon Sep 17 00:00:00 2001 From: Ryan Wilson Date: Sat, 25 Apr 2015 08:05:33 -0700 Subject: [PATCH] use replicaset if configured Signed-off-by: Ryan Wilson --- src/database/mongo.js | 11 ++++++++++- tests/mocks/databasemock.js | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/database/mongo.js b/src/database/mongo.js index be473e616c..1632d41334 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -73,7 +73,16 @@ nconf.set('mongo:database', '0'); } - var connString = 'mongodb://' + usernamePassword + nconf.get('mongo:host') + ':' + nconf.get('mongo:port') + '/' + nconf.get('mongo:database'); + var hosts = nconf.get('mongo:host').split(','); + var ports = nconf.get('mongo:port').split(','); + var servers = []; + + for (var i = 0; i < hosts.length; i++) { + servers.push(hosts[i] + ':' + ports[i]); + } + + var connString = 'mongodb://' + usernamePassword + servers.join() + '/' + nconf.get('mongo:database'); + var connOptions = { server: { poolSize: parseInt(nconf.get('mongo:poolSize'), 10) || 10 diff --git a/tests/mocks/databasemock.js b/tests/mocks/databasemock.js index d3dea94a8c..a895d25c8a 100644 --- a/tests/mocks/databasemock.js +++ b/tests/mocks/databasemock.js @@ -45,6 +45,14 @@ ' "password": "",' + '\n' + ' "database": "1"' + '\n' + '}\n'+ + ' or (mongo) in a replicaset' + '\n' + + '"test_database": {' + '\n' + + ' "host": "127.0.0.1,127.0.0.1,127.0.0.1",' + '\n' + + ' "port": "27017,27018,27019",' + '\n' + + ' "username": "",' + '\n' + + ' "password": "",' + '\n' + + ' "database": "nodebb_test"' + '\n' + + '}\n'+ '===========================================================' ); winston.error(errorText);