简体   繁体   English

流星与多个Mongo数据库

[英]Meteor with multiple Mongo DBs

I've just started with Meteor and MongoDB. 我刚刚开始使用Meteor和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 ? 那么,为什么我做了这个Products['productus' + customerId]而不是如果单纯Products

Here you have to be aware of the MinoMongo db on the client. 在这里,您必须了解客户端上的MinoMongo数据库。 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. 现在,他仅在服务器上使用其他数据库BUT。 On the client you still have only 1 database with 1 collection named Products . 在客户端上,您仍然只有1个数据库和1个名为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. 为了防止这种情况,您需要在客户端上使用2个单独的集合。 To achieve this you add the customerId to the collection. 为此,您可以将customerId添加到集合中。

That's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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