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.
You can dynamically build your models, and also dynamically look up and use your models.
Where you create your models:
for (const customer of customerList) {
mongoose.model<CompanyDocument>(`${customer}_comments`, schema)
}
Then to utilize:
CustomerModel = mongoose.model(`${customer}_comments`)
That said - while this is definitely doable - this is not a good pattern. Mongo should scale for all of your use cases if you properly use indices, with no need for this added complexity. Millions of posts and comments can still be fast and sustainable without breaking into separate collections.