I need store dynamic json in DB and search by key-value
i have json
[{
"id": 1,
"Type": "string",
"FieldName" : "Test",
"Value": "Test",
}, {
"id": 2,
"Type": "bool",
"FieldName" : "Test2",
"Value": "true",
"id": 2,
"Type": "dropdown",
"FieldName" : "dropDownTest",
"Value": [{"text":"my placeholder", "Value": "myTestValue"}, {"text":"my placeholder2", "Value": "myTestValue2"}]
},
{
"id": 3,
"Type": "int",
"Value": 133
},
{
"id": 4,
"Type": "DateTime",
"Value": ""
}]
This json generated by admin-site. Admin has a panel where he can add columns, fieldType and the default value (or array of values if this drop-down)
This json transform to form, which user fill on site and store to DB
Data for saving look like this:
[{
"QuestionId": 1,
"Value": "Test",
},
{
"QuestionId": 2,
"Value": true,
},
{
"QuestionId": 4,
"Value": "true",
"Value": "2018-09-16T20:03:57.551Z"
}
]
Task:
- Store answers for users in DB
- Search by Answers in DB (filters on site, one or multiple matches e.g question 1 and 4 matches (value is "Test" and Date More then Yesterday...))
- Optimal structure for fast search
My Vision: create an entity
public class QuestionEntity
{
public int id { get; set; }
public string Type { get; set; }
public string FieldName { get; set; }
public string Value { get; set; }
}
and create an answer entity
public class Answer
{
public int QuestionId { get; set; }
public string Value { get; set; }
}
And Add this Answers as user collections
public ICollection<Answer> Answers{get; set;}
I not sure what it's an optimal way for this task, and I will be grateful if someone to share their cases if you have them.