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 !
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 .
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.
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.
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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With