Meteor - 连接多个数据库的集合 [英] Meteor - connect collections of multiple databases

查看:20
本文介绍了Meteor - 连接多个数据库的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Meteor(服务器端)中,是否可以创建多个数据库的集合?

In Meteor (server side), is it possible to create collections of multiple databases?

假设我想连接到两个不同的数据库并将它们的集合安装在meteor 中.我担心的是两个数据库中名称相同的集合(例如用户").

Let's say I want to connect to two different databases and mount their collections in meteor. My concern are collections with the same name in both databases (for example "users").

有没有办法让 2 个名为users"的集合来自 2 个不同的数据库(连接)?

Is there any way to have 2 collections named "users" but from 2 different databases (connections)?

谢谢!

另一个问题没有解决我的主要问题:如果我想从 2 个不同的数据库挂载(连接)两个名为users"(例如)的集合,该怎么办.

The other question does not address my main issue: what if I want to mount (connect) two collections named "users" (for example) from 2 different databases.

流星说:

错误:已定义名为/users/insert"的方法

Error: A method named '/users/insert' is already defined

推荐答案

我重新打开了这个问题,但没有简单的答案.mongo 驱动程序假定每个集合有一个连接.顺便说一句,这是一个合理的假设 - 如果您进行了写入,将更新哪个数据库?

I reopened the question, but there is no easy answer. The mongo driver assumes one connection per collection. As an aside, this is a reasonable assumption - if you did a write, which DB would be updated?

这里有一些方法可以让您在不实现自己的驱动程序的情况下解决此限制:

Here are some ways you could work around this limitation without implementing your own driver:

  1. 声明多个集合(Users1Users2),其中每个集合都可以访问其中一个数据库实例.从技术上讲,这可以正常工作,但在您的代码中可能并不容易.

  1. Declare more than one collection (Users1 and Users2), where each collection has access to one of the database instances. Technically this would work fine, but may not be easy to do in your code.

使用外部进程定期将一个集合的内容从 db1 复制到 db2.这允许您使用单个集合,但如果某些文档是从外部应用程序编写的,则可能会变得复杂.

Use an external process to regularly copy the contents of one collection from db1 to db2. This lets you use a single collection, but could get complicated if some of the documents are being written from external applications.

仅使用方法来访问数据,而不是将其发布到客户.您失去了在客户端拥有集合语义的能力,但您可以直接控制数据库的使用方式.另请参阅此问题的答案,了解有关如何直接使用 RemoteCollectionDriver 实例的一些示例.

Only use methods to access the data instead of publishing it to the client. You lose the ability to have collection semantics on the client, but you can directly control how the database is used. Also see the answers to this question for some examples of how to directly use an instance of RemoteCollectionDriver.

这篇关于Meteor - 连接多个数据库的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆