I've created an API for some entities, each of which have their own schema and a corresponding collection. Such as:
posts
authors
comments
There is one to one relationship between posts
and authors
, and one to many relationship between posts
and comments
.
Now the data size is increasing, and we are planning to distribute the data customer wise to speed up query and aggregations. This will mean we will now have following collections:
<customer_1>_posts
<customer_2>_posts
...
<customer_n>_posts
<customer_1>_authors
<customer_2>_authors
...
<customer_n>_authors
<customer_1>_comments
<customer_2>_comments
...
<customer_n>_comments
Since structure is common across all <customer_x>_posts
, and similarly across corresponding authors
and comments
, I'm thinking of using common schema and API.
Now, is there a way to use common Mongoose models and decide the collection for data storage at the save time, depending on the value of the customer field? This would save me from creating a separate model for each client, and also from creating separate endpoints.