MongoDB has the flexible schema. It means, that you can insert documents with completely different structure in your collection. But in this case, you would need to do the data validation on application side, before each write-operation (create, update).
Although, MongoDB supports schema validation, but it will validate all the documents in a collection against the same schema. It means, that you can not have multiple schemas for a single collection.
You can, however, use MongoDB schema validation with different schemas by creating a dedicated collection with its own schema per each user:
const userId = <id>;
That would work, but you can easily reach limitations collection number if your db instance is provided by some Cloud provider. However, if you have deployed MongoDB on your own servers, the limitations are much more compromising. So, use this at with extreme caution and only if you’re sure it will not create any complications in your application code.
Better to think, if you can standardize the data format, coming from your users. This could make your developer’s life much easier: you could use only 1 schema for your single collection and all the validation could be done on database side.
If you can not control user’s payload, then consider this 3-step solution:
Define schema for each user. You can use shema-inspector shemas for schema definitions and validations, if you have Node.js on backend side. If you use other programming language - search a lib, that can support validation of documents, based on predefined schema.
Persist the schemas in some user-related collection. You will need to fetch those schemas each time, when user would want to create/update the product.
Validate user’s payload object before inserting or updating document:
- fetch schema for dedicated user
- validate payload against that schema
- if there are errors, return them to the client
- if everything is OK - write to the collection