0

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

Radko
  • 101
  • 6
  • Should be possible if you supply the connection when creating the Collection instance in Meteor. Publishing might be tricky, though. – MasterAM Nov 01 '15 at 16:33
  • @Radko I believe you can only use one database per collection - so in the cases where you have the same collection in multiple databases, you'd need to pick which one to connect to. – David Weldon Nov 01 '15 at 18:04
  • @David Weldon thanks, but is there no way to hack it around? – Radko Nov 02 '15 at 09:38

1 Answers1

2

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. 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.

  2. 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.

  3. 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.

David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • Hi, thanks for this. We actually went with solution 3. We use Methods and pull data on request from any connection / database we need. – Radko Nov 04 '15 at 12:58