If your data has a one-to-many relationship, you may find that it can't be filtered using standard array filtering.
For example, if your logical structure requires that the position of a value in an array is meaningful, and doesn't just match any value anywhere in the array, a simple array may not work.
Consider this data structure:
{
"carA":[
{
"year":2000,
"make":"Ford"
},
{
"year":2002,
"make":"Lexus"
}
]
}
it could be represented in a two array structure for year and make:
{
"carA":{
"year":[2000,2002],
"make":["Ford","Lexus"]
}
}
Filtering the data like this would be equivalent to filtering with "AND"s on two separate arrays, and it won't incorporate the position in the array.
In another example, a specific object may have many start and end dates associated with it. Or, a specific machine part may be applicable to many different machines.
In cases like this there are two main data structures you can use. We'll explore these now, along with benefits and downsides of each.
For some item A with a relationship with main_item_a and main_item_b, have a record for each
item that it has a relationship with. The con here is that this may be a lot of items.
{
"id":"my_id_a",
"belongs_to":"main_item_a",
"objectId":"uniqueA"
}
{
"id":"my_id_a",
"belongs_to":"main_item_b"
"objectId":"uniqueB"
}
Another way to create this record is:
{
"id":"my_id_a",
"belongs_to_one":"main_item_A",
"belongs_to_two":"main_item_B",
"objectId":"uniqueB"
}
If we relate this back to the initial car example, the first option would look like this:
{
"id":"carA",
"has":"2002",
"objectID":"uniqueIDA"
},
{
"id":"carA",
"has":"Lexus",
"objectID":"uniqueIDB"
}
In this example, "CarA" is an identifier for a vehicle that you want filter by "is type carA and has some value".
The second option would look like this:
{
"id":"carA",
"year":"2002",
"make":"Lexus",
"objectId":"uniqueID"
},
{
"id":"carA",
"year":"2000",
"make":"Ford",
"objectId":"uniqueID"
}
In this example there is an assumption of CarA having many types. The problem here is that you can't as easily add more items to the structure and your filters may be more complex (i.e belongs_to_one = main_item_A OR belongs_to_two = main_item_B, etc).
The best format will depend on how you want to filter your data. There isn't a one size fits all solution for this currently.
You can also review this article on the issue.