@yvervoort Wix Data is a document based database, not a relational. As such, it has advantages and disadvantages.
Relational databases are well known and great products. They are solving all problems using inner joins and transactions - as a hammer that looks for a problem. With other databases, like document databases, we focus on the specific problem at hand and try to give a dedicated solution - trading other advantages and optimizations to the inner joins and transactions of relational DBs.
With Wix Data, we have 3 (or even 4) different options -
-
nested objects
-
single references (1:N)
-
multi references (N:M)
-
Aggregate API
Depending on your need, one of the above tools will probably work. Can you share more details as to what you are trying to solve? I mean, in terms of the outcome you expect, as we need more guidance compared to just equivalent of inner join - as a document DB does not have inner joins.
- One way to work with a document database is to create complex objects with nested array - you can store in the database objects like
{
field_1: ...
field_2: ...
field_3: ...
field_4: [
{field_5: ..., field_6:...},
{field_5: ..., field_6:...},
{field_5: ..., field_6:...}
]
}
With nested objects like the above, you can do all kind of operations, like filter on the child objects with
wixData.query('collection')
.eq('field_4.field_5', some-value)
.find()
which will find any top level item that has one (or more) child elements in field_4 who has field_5 equal to the some-value.
With this approach, you have a guarantee of consistency of the whole top level item.
- Another approach is to model your data with two collections using a reference field. Using a reference field, you have a N:1 relation, similar in some sense to a foreign key, yet different in other regards.
You can do query on the referencing collection, and using the .include operator include the referenced item in the result. You can query the referenced item and using the .include operator get up to 50 referencing items.
e.g.
if we have Album and Track collections with a relationship Track N : 1 Album, query of
Album include Track returns tracks as an array, up to 50 elements.
Track include Album returns the one album referenced.
- Last, you can do multi-reference, which is an N:M relationship, more for cases like Picture : Color or Car Model : Car Part, etc.
With multi-reference, you have a dedicated query model for the relationship, because of the potential of both N and M to be large - using the queryReferenced APIs.
- The aggregate API may also be relevant, depending on your actual need.