Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store dynamic fields in Mongo DB

Tags:

mongodb

nosql

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

My application has a dynamic form builder that lets users add dynamic fields on the form . None of the field on the form is fix or static.

User can add any number and any type of fields like text field , Dropdown or date fields on the form and save the form .

I believe since its a dynamic storage and fields are not static, Oracle DB wont work for storage . Can Mongo DB be used for such storage and fetching the data ?

How the data be stored in Mongo DB and fetched to display the form.

Thanks in advance !

like image 443
user3566578 Avatar asked Apr 23 '14 22:04

user3566578


People also ask

How do I add a field to a collection in MongoDB?

To add field or fields to embedded documents (including documents in arrays) use the dot notation. See example. To add an element to an existing array field with $addFields , use with $concatArrays .

Can we store data in MongoDB?

MongoDB is a NoSQL Server in which data is stored in BSON (Binary JSON) documents and each document is essentially built on a key-value pair structure. As MongoDB easily stores schemaless data, make it appropriate for capturing data whose structure is not known.

How is data in MongoDB stored?

MongoDB stores data objects in collections and documents instead of the tables and rows used in traditional relational databases. Collections comprise sets of documents, which are equivalent to tables in a relational database. Documents consist of key-value pairs, which are the basic unit of data in MongoDB.


2 Answers

MongoDb is Document based Database as you mentioned it has no column or field restriction. You can put your dynamic field into a object and save it to same collection.

for example

 Person: {
   name: '',
   Contacts: [ contact 1, contact 2]
}

you can add more field too for example you can add email to it

 Person: 
 { name: '', contacts: [], email: ''}

You can now save that in Person Collection as well. Just use javascript object and add new key and value and save it.

 person.email = 'new value';

When Fetching those you can iterate over keys on the objects and show the values again to front end

for (var key in person) {
  // do something with key
}
like image 119
sir4ju1 Avatar answered Nov 15 '22 05:11

sir4ju1


Yes, MongoDB can do this. A document to represent such a form could look like this:

{
     tile: "A simple customer survey",
     creator: "Philipp",
     created: ISODate("2014-04-24T16:59:42.389Z"),
     questions: [
         { 
           question: "How old are you?",
           input: "number"
         },
         { 
           question: "Do you like our products?",
           answers: [
               "Yes",
               "No",
               "Maybe"
           ]
         },
         { 
           question: "Which aspects of our products do you like?",
           multiple: true,
           answers: [
               "color",
               "shape",
               "material",
               "price",
               "does not explode often"
           ]
         }
     ]
}

Note that the entries in the "questions" array all have a different combination of fields. Handling this is no problem at all for MongoDB because it does not enforce a consistent schema for the documents in a collection.

like image 42
Philipp Avatar answered Nov 15 '22 05:11

Philipp