Can we/How to connect to multiple mongodb?

if we build several apps sharing some data, such as users, we hope to separate such data into a separate database, can we / how to specify a database to use for a specific collection? or is there some other solution for my situation? thanks.

If they’re meteor applications, you can use DDP.connect as a quick solution.

You can specify the database you want to use through the connection option on a Mongo.Collection.

let server1 = DDP.connect('http://localhost:4000/');
let server2 = DDP.connect('http://localhost:5000/');

let thing1 = new Mongo.Collection('things', { connection: server1 });
let thing2 = new Mongo.Collection('things2', { connection: server2 });

This can be done but is a bit tough.

On the server you can use MongoInternals.RemoteCollectionDriver. Make sure you provide an oplog url in the option oplogUrl. Then when you create a collection, on the server you pass the driver to the _driver option. Note that this is an underscore parameter, so should not be expected to remain unchanged from version to version. This is valid for 1.2 for sure and also for current development branch (so probably for 1.3 as well):

// WARNING: This is untested code built from memory of when I had to solve this myself!
// it should be tested and validated!
let driver = new MongoInternals.RemoteCollectionDriver(url, {oplogUrl});
let collection;
if (Meteor.isServer) {
  collection = new Meteor.Collection('myCollection', {_driver: driver});
} else {
  collection = new Meteor.Collection('myCollection');

There might be:


must go through another app? can it connect to a mongodb directly?

Checkout my answer above, I think it might answer what you need.

thanks for your answer, it will offer a solid option for me.

I found a problem: this way is only possible for new defined collection. how to set db connection of Meteor.users ?

That is not actually possible currently as the constructor for the accounts system sets up the collection, so you can’t control it.

There are a couple of options for that though in the links I posted earlier.

For code reference into why this isn’t an option: line 23

One option if you don’t care about having to login again, is to have the user accounts table, and a few other common tables in a common database which is passed to all apps via MONGO_URL, then use the above technique for app specific data.

this is a good idea. more, I hope meteor will have its microservice system in the future.

There is one. In my first reply, I linked to two packages. One is cluster which is a load balancing and microservice implementation. The other handles accounts through the cluster.

As @lassombra mentioned you can use MongoInternals. I packaged up the logic in Probably the simplest solution if you don’t need anything fancy…

looks great. thanks. I may try it in next refactory.

Cool. Let me know if you have any probs.

hi @maxfi , I have submitted a pr to support oplog.

Hello :slight_smile:,
You can use MongoInternals as mentioned in above reply. If you don’t want to do complex here is a simple solution package about MongoDB.

Thanks @zhaoyao91. I’ll check it out in the next couple of days.