问题
In Meteor (server side), is it possible to create collections of multiple databases?
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").
Is there any way to have 2 collections named "users" but from 2 different databases (connections)?
Thanks!
EDIT:
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.
Meteor says:
Error: A method named '/users/insert' is already defined
回答1:
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:
Declare more than one collection (
Users1
andUsers2
), 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.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.
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
.
来源:https://stackoverflow.com/questions/33464238/meteor-connect-collections-of-multiple-databases