2

I'm building a Meteor Web API using this lib, and one of the features of the Web API is that it must be able to connect any one of several databases based on a Web Request from a Meteor client.

.

I understand it's now possible to connect to multiple databases from one Server Side Only Meteor application from this SO answer (Using Multiple Mongodb Databases with Meteor.js):

It is now possible to connect to remote/multiple databases:

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

Yet, does this work, does it work if you have the same collection names in both databases? The databases are basically replicas, of course with different MONGO_URLs and with different data.

.

For example:

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

.

Can this work with the same collection names? If so how would that work? Also, what if I had two requests for two different database come in at the same time?

A diagram: enter image description here

Aaron
  • 3,068
  • 2
  • 21
  • 44

1 Answers1

3

You should define what a "collection name" is.

So within this code:

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 });

From your "meteor app" perspective, MyCollection and MyCollection2 are two different javascript constants and therefore two different "meteor collections". Based on how javascript works, they cannot be named the same.

But since they connect to two different databases and different databases are completely different things (unless the two mongo url strings are the same), their "database collection" names can be the same. They still are two different collections in two different databases.

From a database perspective, concurrency does not matter either. They can respond to concurrent database calls just fine because they are different databases. But then again, your javascript application code must be able to manage this concurrency and handle its results and exceptions, because javascript is not threaded, but evented.

Serkan Durusoy
  • 5,447
  • 2
  • 20
  • 39