Architecture: Multiple Mongo databases+connections vs multiple collections with Express

3.1k views Asked by At

I am building an app that stores sensitive data for severals different customers in Mongo DB. The data model is the same for each client (emails, contacts, meetings). All clients access the data using the same API with the same Express server. I have read a lot about using one large collection vs several collections vs several databases:

Mongoose and multiple database in single node.js project

MongoDB performance - having multiple databases

I like the idea of using one database per client because of security and simple seggregation of the data. Am I right to think like that?

Also in this scenario I am a bit concerned about managing databases connections in Express.

I understand that the connection should only be made once (e.g. when starting the server) and then kept alive. So using several databases would mean that Express opens several connections and keeps them alive, maybe dozens or hundreds in the future. Also, with every api call the controller should be able to chose which database connection to query from.

Is it worth the trouble?

1

There are 1 answers

0
Nopik On BEST ANSWER

For multi-tenant app like this, I would recommend going for storing similar data in single collection for all tenants, with some kind of tenant_id field inside, then making sure that all queries have proper selector.

If you go for multiple collections, or worse, multiple databases, it wont be too scalable. If you have just few clients and do not plan to have hundreds or more of them, then well, multi collection or multi db approach could be workable. I guess it will be painful, though. Mongo is going to allocate lots of disk space for each collection, extra connections will take more memory etc., the performance hit will not be negligible.