简体   繁体   中英

Meteor with multiple Mongo DBs

I've just started with Meteor and MongoDB. I was wondering if there is a way to use two or more DBs at once. These DBs have to be generated on runtime.

The idea is that I have a few user groups which are absolutely not allowed to access data of the other user group. If there is another way tell me.

Well I have done it...long ago but I still want to post the answer here.

What you need is something like this:

dbCustomer = new MongoInternals.RemoteCollectionDriver(
    CUSTOMER_DB_URL + customerId
);

This way you create your custom driver which you use while creating a new collection:

Products['procucts' + customerId] = new Meteor.Collection(
    'products' + customerId,
    {
        _driver: dbCustomer,
        idGeneration: 'STRING'
    }
);

So why have I done this Products['productus' + customerId] instead if simply Products ?

Here you have to be aware of the MinoMongo db on the client. The client doesn't care about which driver you are using (use the driver only on server side). Imagine the case a user logs out and logs in to another customer. Now he is working with a different database BUT only on the server. On the client you still have only 1 database with 1 collection named Products . What know? Exactly! You have both data the ones from the old customer and the ones from the new customer.

That's not a problem of data security but you will see both data too (which is clearly false). To prevent this you need 2 separate collections on the client. To achieve this you add the customerId to the collection.

That's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM