Architecture: Multiple Mongo Databases + Connections and Multiple Collections with Express
I am creating an application that stores sensitive data for individual clients in Mongo DB. The data model is the same for every client (emails, contacts, appointments). All clients access data using the same API with the same Express server. I've read a lot about using one large collection versus multiple collections and multiple databases:
Mongoose and multiple databases in one nodejs project
MongoDB performance - having multiple databases
I like the idea of using one database per client because of the security and easy data sorting. Am I right to think so?
Also in this scenario, I am a little concerned about managing database connections in Express.
I understand that the connection only needs to be done once (for example, when starting the server) and then stay alive. So using multiple databases means Express opens multiple connections and keeps them alive, maybe tens or hundreds in the future. In addition, on every api call, the controller should be able to choose which database connection the request is required to.
Should you worry?
source to share
For multi-tenant applications like this, I would recommend storing the same data in a single collection for all tenants with some kind of tenant_id field inside, and then making sure all requests have the correct selector.
If you are going to use multiple collections, or worse, multiple databases, it won't be too scalable. If you only have a few clients and don't plan on having hundreds or more of them, well, a multi-user approach or a multi-db approach might work. I think it will be painful. Mongo is going to allocate a lot of disk space for each collection, additional connections take up more memory, etc., the performance hit won't be negligible.
source to share