gpt4 book ai didi

javascript - Meteor Server-only Web App 连接到多个数据库

转载 作者:可可西里 更新时间:2023-11-01 10:02:30 26 4
gpt4 key购买 nike

我正在使用这个 lib 构建一个 Meteor Web API ,Web API 的特性之一是它必须能够根据来自 Meteor 客户端的 Web 请求连接多个数据库中的任何一个。

.

我知道现在可以通过这个 SO 答案(Using Multiple Mongodb Databases with Meteor.js)从一个仅服务器端的 Meteor 应用程序连接到多个数据库:

现在可以连接到远程/多个数据库:

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

但是,如果两个数据库中的集合名称相同,这是否有效?数据库基本上是副本,当然具有不同的 MONGO_URL 和不同的数据。

.

例如:

(1) A Web Request from a client would come into the Meteor Web API. That request would contain data that specified a database ID=2. I would look up that ID and match it to a database URL. I would connect to that database and pull in the collection name "People" and do processing on that data.

.

(2) Another Web Request from a different client comes into the Meteor Web API. This time with a database ID=4, the Web App looks up the database and connects to another, different, URL, and pulls in the same collection name, "People", yet this time of course it has different data (because of course it's a different database).

.

这可以使用相同的集合名称吗?如果是这样,那将如何工作?另外,如果我同时收到对两个不同数据库的两个请求怎么办?

一张图: enter image description here

最佳答案

您应该定义什么是“集合名称”。

所以在这段代码中:

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

const database2 = new MongoInternals.RemoteCollectionDriver("<mongo url 2>");
const MyCollection2 = new Mongo.Collection("collection_name", { _driver: database2 });

从“ meteor 应用程序”的 Angular 来看,MyCollectionMyCollection2 是两个不同的 javascript 常量,因此是两个不同的“ meteor 集合”。根据 javascript 的工作原理,它们不能被命名为相同的。

但是由于它们连接的是两个不同的数据库,不同的数据库是完全不同的东西(除非两个mongo url字符串相同),所以它们的“数据库集合”名称可以相同。它们仍然是两个不同数据库中的两个不同集合。

从数据库的 Angular 来看,并发性也无关紧要。它们可以很好地响应并发数据库调用,因为它们是不同的数据库。但话又说回来,您的 javascript 应用程序代码必须能够管理这种并发性并处理其结果和异常,因为 javascript 不是线程化的,而是事件化的。

关于javascript - Meteor Server-only Web App 连接到多个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45148450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com