2
votes

I currently assign a mongodb to my meteor app using the env variable "MONGO_URL": "mongodb://localhost:27017/dbName" when I start the meteor instance.

So all data gets written to the mongo database with the name "dbName". I am looking for a way to individually set the dbName for each custumer upon login in order to seperate their data into different databases.

2
Why do you want to separate customer data into separate databases? - Brett McLain
@BrettMcLain because my customers are businesses themselves. They will then be able to create their own users within their designated database. It is generally good practice to run seperate DBs when offering a B2B solution. One advantage would be that no other users are affected if one of my customers needs to restore their data from a backup. - Chris

2 Answers

1
votes

This generally unsupported as this is defined at startup. However, this thread offers a possible solution:

https://forums.meteor.com/t/switch-database-while-meteor-is-running/4361/6

var database = new MongoInternals.RemoteCollectionDriver("<mongo url>"); 

MyCollection = new Mongo.Collection("collection_name", { _driver: database });

This would allow you to define the database name in the mongo url but would require a fair bit of extra work to redefine your collections on a customer by customer basis.

0
votes

Here's another approach that will make your life eternally easier:

  1. Create a generic site with no accounts at mysite.com
  2. When they login at mysite.com, figure out what site they actually belong to and redirect them to customerName.mysite.com and log them in there
  3. Run a separate instance of Meteor configured for a different mongo at each site

nginx might help you with the above.

It is generally good practice to run separate DBs when offering a B2B solution.

That's a matter of opinion that depends heavily on the platform. Many SaaS providers would argue that point.