0

I have a node.js (and express) product administration app which connects to a mongodb database containing the produt data, and uses Stormpath for user administration.

This was all fine, but now I need the app to be able to connect to two different databases - one for UK products and one for US products. The users need to be able to switch between the UK and US databases whenever they like.

I have been able to make this work for the whole app just by adding a button which disconnects from one db and connects (using MongoClient) to the other, but what I need is to either have two connections at all times, and pick which one to use based on which country the user has selected, or to have a db connection per user.

Which is the correct way to do this sort of thing?

1 Answer 1

1

I think you can spawn 2 active mongodb connections, it is possible. According to - npm module of mongodb documentation

var 
  mongo = require('mongodb'),
  MongoClient1 = mongo.MongoClient,
  MongoClient2 = mongo.MongoClient;


MongoClient1.connect('mongodb://somehost1:27017/db1', function(err, db) {
   //do things
});

MongoClient2.connect('mongodb://somehost2:27017/db2', function(err, db) {
   //do things
});

than you can dependency inject this database clients into the request. The reconnection to database can take time, and users can experience a bad performance. I think it is better to keep 2 active connections alwayes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.