背景
我正在开发一个需要用户创建和填写自定义表格(类似HTML或MS Word表格,而非数据库表格)的Web应用程序。管理员将创建这些表格的模板,普通用户将填写它们。每个普通用户可以填写自己的模板,因此将使用许多使用相同模板的表格。
现在,这些模板可以包含任意数量的列,并且每个列可以具有不同的数据类型。它们还可以随时间变化,这意味着应该有它们的版本。换句话说,一旦用户填写了模板,即使模板在未来发生更改,也应该以该结构存储。它将保留模板的旧版本,而新用户将使用最新版本。
我通常使用关系型数据库,但对于这种情况,关系型数据库似乎不是很合适。我觉得如果数据库进行规范化,就会得到一堆表格,并需要许多连接才能提取所需的数据。
我考虑使用像MongoDB这样的东西。我对Mongo不太熟悉,所以我肯定没有探索每个选项,但我认为这比使用MySQL(我用于其他应用程序)要好。不过,如果我错了,请告诉我;也许我漏掉了什么。
我想能够存储模板(名称,列(按顺序,带有其名称和数据类型)),然后每当用户想要填写它们时,就可以重复使用这些模板。我将使用一个集合来存储所有使用相同模板版本的表格。也就是说,每次制作模板时都会创建一个集合。集合应具有所有文档必须遵循的定义数据类型。如果我理解正确,MongoDB为此提供模式验证。
我的方法和问题
How can I store templates in the database? I thought about storing them as documents in a "templates" collection, which would be something like the example below, but I'm concerned about having the data types as strings.
{ name: <String>, fields: [ { name: <String>, type: <String> <----this is what I have an issue with }, ... ] }
I would have to read these templates and convert their structure into HTML tables so users can fill them, then store them as documents in the collections with the template name or id.
Something like:
{ template_id: <uuid, integer, string or something> name: <String>, rows: [ { <field name>:<field value of specified type>, ... } ] }
How feasible and recommendable is it to create collections on the fly?
如果有任何不清楚的地方,请让我知道,这样我就可以澄清。正如我所说,我对MongoDB(以及非关系型数据库)还很陌生,所以如果我漏掉了一些显而易见的东西,欢迎指出并提供建议。文档或教程的链接也很有帮助。我也愿意尝试其他NoSQL解决方案。