如何在MongoDb中存储动态生成的表单的结果? [英] How to store results from dynamically generated forms in MongoDb?

查看:1437
本文介绍了如何在MongoDb中存储动态生成的表单的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MongoDB的新手,但正在寻找它来解决此问题:

I am new to MongoDB, but am looking at it to solve this problem:

我的应用程序具有一个动态表单生成器,该表单生成器允许用户创建自定义调查,联系表单等.我想记录所有表单提交,并让创建表单的用户搜索并导出其提交的数据.

我来自典型的PHP/mySql背景,看到了将数据存储在mySql数据库中的一些挑战.每个表单可以具有任意数量的所有类型的字段.我要么需要将数据库规范化为EAV结构来存储数据,要么需要为每个表单动态创建一个新表,或者将表单数据序列化为TEXT列(点击).

I come from a typical PHP/mySql background, and see some challenges in storing this data in a mySql database. Each form could have any number of fields of all types. I would either need to normalize my DB to an EAV structure to store the data, or dynamically create a new table for each form, or serialize the form data into TEXT columns (ick).

MongoDb的无schema-less"(或动态模式")性质似乎是解决我的问题的完美解决方案,但是我的n00b-iness使我不确定从哪里开始.

The "schema-less" (or "dynamic schema") nature of MongoDb seems like the perfect solution to my problem, but my n00b-iness leaves me unsure where to start.

  1. 每个自定义表单的结果都应存储为单独的集合吗?
  2. 我应该有一个表单"集合,并将结果作为每个表单的子文档嵌入吗?
  3. 还有另一种更好的方法吗?
  4. MongoDb实际上是一个很好的解决方案,还是我偏离了基础?

再次重申我的问题:我需要以一种易于搜索和排序的方式存储可变结构和未知结构的数据.

To restate my problem one more time: I need to store data of variable and unknown structures in an easily searchable and sortable way.

谢谢!

推荐答案

我不会将结果存储为form文档中的嵌入式文档,因为您可能不知道先验有多少提交期待. MongoDB将每个文档的大小限制为16MB,但实际上,您可能希望远低于此阈值.

I wouldn't store results as embedded documents within the form document, since you may not know a priori how many submissions to expect. MongoDB limits each document to 16MB, but in practice you probably want to stay well below this threshold.

由于您的表单是可变的,但是是预先确定的(也就是说,每种表单可能有所不同,但是这些表单是在某种类型的管理UI中提前定义的),所以我建议使用两个集合:

Since your forms are variable, but pre-determined (that is, each form may differ but the forms are defined ahead of time in some sort of admin UI), I'd recommend using two collections:

第一个(称为forms)将存储有关每种形式的构成的数据:什么字段,什么类型,以什么顺序等等.您可以想象该集合中的文档看起来像这样:

The first (call it forms) will store data about the makeup of each form: what fields, what types, in what order, etc. You could imagine documents in this collection would look something like this:

{ _id: ObjectId(...),
  name: "...",
  // other fields, for permissions, URL, etc
  fields: [
    { name: "username",
      type: "text",
      validation: { required: true, min: 1, max: null },
    },
    { name: "email",
      type: "text",
      validation: { required: true, min: 5, max: null, email: true },
    }
  ]
}

这使您可以根据需要动态构造表单(以及一些服务器端代码),以在应用程序中显示.它还提供有关字段是什么以及字段需要什么验证的信息,您可以在表单提交期间使用这些信息.您需要在URL或用于确定在提供Web请求时显示哪种表单的任何字段上建立索引.

This lets you construct the forms dynamically (along with some server-side code) as needed for display in your application. It also gives information about what the fields are and what validation is required for them, which you can use during form submission. You'll want an index on URL or whatever field you use to determine which form to display when serving web requests.

第二个集合submissions或类似的东西,将存储每个表单的提交数据.文件看起来像:

The second collection, submissions or something, would store the submitted data for each form. Documents would look like:

{ _id: ObjectId(...),
  form: ObjectId(...), // the ObjectId of the record in "forms"
                       // that this is a submission on
  // other information here about the submitter:
  // IP address, browser, date and time, etc
  values: {
    username: "dcrosta",
    email: "dcrosta@awesomesite.com",
    //any other fields here
  }
}

如果您需要能够按提交表单中的字段-值对(或仅值)进行搜索,则此格式的变体对values字段使用数组,例如:

If you need to be able to search by field-value pairs (or just values) in the submitted forms, a variation on this uses an array for the values field, like:

{ ...
  values: [
    { name: "username", value: "dcrosta" },
    { name: "email", value: "dcrosta@awesomesite.com" }
  ]
}

然后您可以在values字段上创建索引,并进行类似的搜索:

You can then create an index on the values field, and search like:

// find "dcrosta" as username
db.submissions.find({values: {$elemMatch: {name: "username", value: "dcrosta"}}})

或者在"values.value"上创建索引,然后像这样进行搜索:

Or create an index on "values.value" and search like:

// find "dcrosta" as value to any field
db.submissions.find({"values.value": "dcrosta"})

这篇关于如何在MongoDb中存储动态生成的表单的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆