How to you build a NoSQL model and index (preferably for RavenDb v4) for the following relational schema?
Document type
Contact
, where each record can have multiple additional properties (type of the property is defined inCustomField
and the value inContactCustomField
)
Considering a need to filter/sort on the highlighted fields in one query (all fields from the Contact plus custom fields).
Possible options as I see:
Option #1
Naturally, I'd imagine the following persistent models:
public class Contact
{
public string Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
// Where the key is CustomField.Id and the value is ContactCustomField.Value
public Dictionary<string, string> CustomValues { get; set; }
}
public class CustomField
{
public string Id { get; set; }
public string Code { get; set; }
public string DataType { get; set; }
public string Description { get; set; }
}
However, building an index for a query like below (sorry for a mixed syntax) puzzles me:
SELECT Name, Address, Phone, CustomValues
FROM Contact
WHERE Name LIKE '*John*' AND CustomValues.Any(v => v.Key == "11" && v.Value == "student")
Option #2
Another approach would be keeping a normalised structure (as shown on the picture above). Then it would work - I'd just have to include ContactCustomField
in the query for Contact
.
The downside would be not utilising benefits of NoSQL.