Remote Database has the same collection name

After I learned about the ability to connect to a remote database and pull in collections, I grew drunk with power.

You can connect like so:

var database = new MongoInternals.RemoteCollectionDriver(“mongo url“);
OtherUsers = new Mongo.Collection(“users”, { _driver: database });

The problem is that the collection that is created (off the remote db) has the same database name “users” as my local “users” table that gets created by Accounts, which leads to meteor purple vomit on the terminal.

So to make the problem concrete, lets say that I have a remote database with a mongodb collection called “users”. What is the easiest way to pull that collection into my app while renaming it to something like “remote_users”? The renaming is necessary because my meteorjs app already has a local “users” collection that gets created by Accounts.

Any ideas?

I’m obviously hoping to avoid going all the way down to the nodejs mongodb driver level to get this done. I’m hopeful that there is an easy way to grab a remote collection, and rename the mongodb collection name to something different locally.

1 Like

I’m looking to do this as well, any chance you’ve found a way?

Hey mstanton,
Yeah after digging through the Meteor source code I was able to pull down the Meteor.users collection from another database like so.

Basically after you have a
var remote = ‘mongoURL’;
database = new MongoInternals.RemoteCollectionDriver(remote);

You can perform database operations with your database object, like so.

database.mongo.findOne(‘users’, “selector_object”)

An example might be

database.mongo.findOne(‘users’, {emails : {$elemMatch : {address : “email_string”} } } )

In other words, the database connection exposes a “mongo” object which you can then call normal “find”, “findOne” methods on.

The only difference between these “find”, “findOne” methods is that you need to supply the mongodb collection name as the first argument (as a string).

Don’t ask how many hours it took to figure that one out.

If that doesn’t make any sense feel free to holla back on this thread or shoot me a ring 267 992 4035. I’d be glad to help someone not waste a bunch of time.

–Dan Crescimanno

6 Likes

Finally got around to working on this project again… this is what I was looking for! Thanks!