Different mongo databases with same collection name issue


#1

I’m having some issues trying to access a collection that is named the same, but in different databases.

I’ve read a lot and it seems that there is _supressSameNameError setting that I can pass to the collection parameters, but it’s just not working correctly.

I have a collection on a remote server called ‘users’. It’s a collection that is managed by a different application altogether - not a Meteor app. I need to read this collection.

In my Meteor app, I’m using the accounts package and I’ve reassigned the ‘users’ collection by doing a Meteor.users = Account.users. I’ve also defined two remote databases (I’m not using anything local). Code example below:

var driver = new MongoInternals.RemoteCollectionDriver(
        mongoURL, {
            oplogUrl: oplogURL
        });

var options = {
        _driver: driver,
        _suppressSameNameError: true
    };

var driver2 = new MongoInternals.RemoteCollectionDriver(
        mongoURL2, {
            oplogUrl: oplogURL2
        });

var options2 = {
        _driver: driver2,
        _suppressSameNameError: true
    };

Accounts.users = new Mongo.Collection('portalusers', options);
Meteor.users = Account.users;

var SSOUsers = new Mongo.Collection('users', options2); <--This is where I get the error, even though there is no other 'users' collection defined anywhere.

When I start up Meteor, I get the ‘There is already a collection named “users”’ error in the console and the app won’t run.

I was under the assumption that the _supressSameNameError flag in the Mongo.Collections options would suppress/bypass this error message and let the app run.

As a test, I tried defining a different collection thinking it was an issue with the 'user’s collection.

var T1 = new Mongo.Collection('test', options);
var T2 = new Mongo.Collection('test', options2);

But the same error was thrown - indicating that there was already a collection named test.

I’m on Meteor 1.5.2.2

Any suggestions? Am I doing something wrong with the flag?


#2

You can do var T2 = driver2.open('test'); but it won’t behave exactly the same as T1.

One example is that T2.insert() will generate an _id using an ObjectID instead of a String.


#3

Thanks for the suggestion - I tried it and it didn’t work as expected.

When I subscribe to the collection (T2 in this example), nothing happens. I don’t get any data.

Is using a driver2.open solution just like using the Mongo.Collection call?


#4

You can’t have two different collections with the same name in minimongo so you’ll instead need to use custom publication logic to publish to a different collection name. This is an incomplete example. You’ll want to stop the observer when the subscription ends and possibly more. Just a quick sketch of a thought.

serverside

Meteor.publish('secondCollectionWithSameName', function() {
  const self = this;
  driver2.open('test').find().observeChanges({
    added(doc, etc, etc) {
      self.added('test2', doc._id, doc);
    },
    //
  });
});

clientside

const T2 = new Meteor.Collection('test2');
Meteor.subscribe('secondCollectionWithSameName');

edit: follow-up reading