1

I have three database i.e, main_db it is default load database. I want load database after login. Database are:-

main_db

->user_collection

psm_2017_db

->abc_collection

->xyz_collection

psm_2018_db

->abc_collection

->xyz_collection

Here is my project structure

enter image description here

here is my login script.

client
 |->login
  |->login.js

            Template.login.rendered = function(){
            SessionStore.set("login_user",false);
            };
            Template.login.events({
                'submit #formLogin': function (event, target){
                    event.preventDefault();
                    var email = target.find('#loginEmail').value;
                    var password = target.find('#loginPassword').value;
                    // console.log(email +" "+password);
                    Meteor.loginWithPassword(email, password, function(err){

                        if(err){
                        console.log(err);
                            alert("Invalid Login!");
                        }
                        else {
                        SessionStore.set("login_user",true);
                            console.log('successfully')
                            Router.go("/dashboard")
                        }
                    });
                }
            });

            Template.layout.helpers({
            "isLoggedin": function () {
                return SessionStore.get("login_user");
            }
            });

here is my load collection file

lib
 |->collection.js

     abcCollection=new Mongo.Collection("abc_collection");         
     xyzCollection=new Mongo.Collection("xyz_collection");
vineet
  • 13,832
  • 10
  • 56
  • 76
  • Possible duplicate of [Using Multiple Mongodb Databases with Meteor.js](https://stackoverflow.com/questions/20535755/using-multiple-mongodb-databases-with-meteor-js) – blueren Dec 05 '17 at 09:20

1 Answers1

3

You can connect to multiple dbs using the below approach.

var database = new MongoInternals.RemoteCollectionDriver("<<mongo url>>");
MyCollection = new Mongo.Collection("collection_name", { _driver: database });

<<mongo_url>> is your standard mongodb url. Eg. mongodb://127.0.0.1:27017/database_name

Now, in your specific scenario, main_db contains the user collection (I'm under the assumption that this is pertaining to meteor user collection). You need to have this loaded at all times. You can't have it load after login since user information - which is required for logging in resides in that db!

Once you take care of the above, connecting to the remaining two dbs can be done on login as below:

/lib/dbconnection.js (this will be common to both server and clinet)

Meteor.methods({
    loadDB: function(){

        if(Meteor.userId()){ // if a user has logged in

            var database = new MongoInternals.RemoteCollectionDriver("<<mongo url>>");
            MyCollection = new Mongo.Collection("collection_name", { _driver: database });

        }


    }
})
Meteor.call("loadDB");

loadDB will get called each time a user logs in. But I fear that it will be run each time any user logs in. In order to avoid it being re-initialized for each user login, you might want to do a check on whether database or myCollection already exists.

blueren
  • 2,730
  • 4
  • 30
  • 47
  • i want to load database dynamic. e.g `mongodb://127.0.0.1:27017/` – vineet Dec 05 '17 at 10:46
  • I suppose you can wrap it in a function and call it when the user logs in? This will have to exist in common with the client and server. – blueren Dec 05 '17 at 10:54
  • yes, but how to do this. Because meteor load `lib` directory at startup phase. – vineet Dec 05 '17 at 11:35
  • Updated my answer addressing this. – blueren Dec 05 '17 at 13:01
  • when i refresh web page then this error `"Exception while invoking method 'loadDB' Error: There is already a collection named ...."` generate at server side. how to handle this – vineet Dec 11 '17 at 05:57
  • It's what I feared and explained in the last part of my answer. You can have only one instance of a collection declared. You will have to have something like an if block which checks to see if the collection is already existing. If it's not, only then, proceed to initialize the connection. – blueren Dec 11 '17 at 06:49