Node Express APP 1 to N (with MongoDB) Node Express APP 1 to N (with MongoDB) database database

Node Express APP 1 to N (with MongoDB)


You don't need to manage multiple instance as you can create a company collection and in that collection you can store every single company and then you just need to create a reference of all these values in users.Please make sure that you have made unique index on company collection.It is really easy handle such scenarios in RDBMS(mysql).

And one more thing you can also run multiple mongod client on same instance by just changing the port and if you are looking for that sort of solution then you can do that as well.

Please note following things before using mongo:-

Please use mongo only if you have over TB's of data because that doesn't make any sense to use mongodb for some mb's or gb's of data.

Use of indexes is must in mongo if you want maximum performance.

Mongo stores all the indexes in main memory and if the indexes size is more then memory that it start swapping of indexes which is really costly and hence please make sure that you have different servers for your application and your db.

I still says it would be better to use RDBMS if you don't have TB's of data to deal with.Why this approach:-

Let me give you a scenario.You have 100 companies and with in 100 companies you have 1000 users for each of the company. i.e. you have 1L records in your user collection.Now i want to delete a single user or i want to update a user or i want to fetch a user from a single company then i don't need to traverse my complete database as i can make a index on my user collection using user-id and company id(compound index) or even i can make a simple filter query on company id.

For index please read thishttps://docs.mongodb.com/manual/core/index-compound/

And btw we are not saving company id as an object instead i am saving only the value of _id from company collection.