Help! How to query/filter data from referenced collections

I can’t figure out how to get field values from joined collections. How do you do this in wixData.Query( ) and in $w(“#myDataset”).setFilter( ) scenarios?

It looks like ‘_items’ collections that are returned hold a ‘ref’ field with an internal primary key to the referenced collection, but I do not know what to do with that?

Thanks!

Hi,
That ref is “_id” field of the item in the referenced collection.
You can use wixData.query(“nameOfCollection”).eq(“_id”,the ref that you received).find()
And then you get the item from the referenced collection.
Roi

This is somewhat helpful - thank you. However, I am still struggling. I have 2 collections: ProgramTypes and CoursePrograms. ProgramTypes is a lookup collection. CoursePrograms has a reference back to ProgramTypes (via a Reference field named ‘Program’). My code has the ‘_id’ from the ProgramTypes collection already. Now, I need to query the CoursePrograms collection where the Program.ref = [my _id].

var id = “ABC123”; //_id from ProgramTypes
wixData.query(“CoursePrograms”).eq(“program.ref”, id).find();

This does not work. How can I get this working?
Thanks!

Please help - I am stuck and need to keep moving. Thanks!

Am having a similar issue at the moment with a filter. Wish I could help… Sometimes it helps to know we are in the same boat!

Hi Ben,
Try this:
wixData.query(“CoursePrograms”).eq(“_id”, id).find();
Please update us with your progress.
Roi

Hi Christine,
Can you please share your code ?
Roi

Hi Roi,

So in mine I’m trying to link a dropdown (“dropdown1”) to set a filter on a dataset’s reference field.

export function dropdown1_change(event, $w) {
let $index = $w(“#dropdown1”).value;

if ($index === ""){ 
	$w("#dataset2").setFilter( wixData.filter()); 
} 
else{ 
$w("#dataset2").setFilter( wixData.filter() 
.contains('_id', $index));} 

}

Christine

Hi Christine,
Can you please share a link to your site so i can inspect?
Roi

https://syverts2.wixsite.com/mysite

Hi,
First thing is that you need to change the filter for an input that is equal to the “university” field and not “_id” like this:

$w("#dataset2").setFilter( wixData.filter()
	.eq('university', $index));}

I checked it out and it doesn’t work, i’m afraid it is because the setFilter function doesn’t work on reference fields, I will give you an answer soon.

darn… yea, that’s what I had before and it wasn’t working

Hey Or, were you able to figure out if the setFilter function works on reference fields?

I figured out how to get it to work!!!

Hi Christine,
could you share the code with the solution for your problem?
Thanks in advance !!

Hi Christine, can you please share how you were able to make this work?

Don’t you just love it when you are searching for something… and someone has the solution but they don’t help you???

You are searching for a solution?
Then you should perhaps open an own new post with your own topic.

Who is that bad guy who do not want to help? :grin:
Do you mean the post-writer above you, which already are gone or even dead by CORONA-VIRUS? (JOKE) :wink:

Take a look at the date, when the last post was released.
You will wait for month or even years, for an answer, because none of these persons are still here (perhaps).

And also take a look into the CORVID-Community-Guidelines…
https://www.wix.com/corvid/forum/community-discussion/corvid-community-guidelines

This may help you to prevent such situations again and will save you a lot of waiting time :wink: