Two collection in same data query on basis of reference key

Hi, I want to ask a question related to wix code. How can we combine two collection of database while writing a wix data query? Like for example there is a query

import wixData from ‘wix-data’;
wixData.query(“Customer”)
.gt(“age”, 20)
.ascending(“name”)
.limit(15)
.find()
.then( (results) =>
{ console.log(results.items); } );

I want another collection in the same query which has a reference field in the Customer Collection. Basically I want to get combined data from both collection. Like we combine two tables in sql database on the basic on foreign key. I want to apply same idea here too. I will be very thankful, please guide me.
Thanks
Awais

1 Like

I’m looking for the exact same thing - query joined collections (joined on reference field).

Datacollections allow you to access any field from the linked tables, but wix data query does not seem to support this from what I can tell.

Worst case scenario is going to be finding all the distinct values of your reference field in customer table, and then querying the reference table separately. That would be ridiculously inefficient though.

Actually there is a way to include the referenced collection: http://www.wix.com/code/reference/wix-data.WixDataQuery.html#include . Just got it to work. Not thrilled about how the data from the referenced collection is nested as it is, but at least you can access it.

1 Like

Thanks a lot Peter. I think this will work.

This link now returns 404 Not Found. Any idea what happened to it?