Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wouldn't store results as embedded documents within the <code>form</code> document, since you may not know <em>a priori</em> how many submissions to expect. MongoDB limits each document to 16MB, but in practice you probably want to stay well below this threshold.</p> <p>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:</p> <p>The first (call it <code>forms</code>) 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:</p> <pre><code>{ _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 }, } ] } </code></pre> <p>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.</p> <p>The second collection, <code>submissions</code> or something, would store the submitted data for each form. Documents would look like:</p> <pre><code>{ _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 } } </code></pre> <p>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 <code>values</code> field, like:</p> <pre><code>{ ... values: [ { name: "username", value: "dcrosta" }, { name: "email", value: "dcrosta@awesomesite.com" } ] } </code></pre> <p>You can then create an index on the <code>values</code> field, and search like:</p> <pre><code>// find "dcrosta" as username db.submissions.find({values: {$elemMatch: {name: "username", value: "dcrosta"}}}) </code></pre> <p>Or create an index on "values.value" and search like:</p> <pre><code>// find "dcrosta" as value to any field db.submissions.find({"values.value": "dcrosta"}) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload