Collection Reference fields filter (search)

Hi, I’m working on a property search and I can’t get the Collection Reference fields to filter the search. There are various dropdown fields (all with manually inserted values) which filter data in a repeater. Two of these fields need to filter a Reference field each.

So, I have a collection called ‘Properties’ which has all the information stored in it. Some of the fields are referenced to other collections – eg: ‘location’ field references to the ‘Localities’ Collection, and ‘propertyType’ field references to the ‘ProperType’ Collection.

I cannot get the Reference fields filter code to work. The other fields in the search filter work fine. I’m missing something and I cannot understand how to make it work. Can anyone please help?

This is the main database collection ‘Properties’ – the ‘location’ and ‘propertyType’ reference fields are in it:

This is one of the Reference database collection ‘Localities’. Field name: ‘locality’.

These are the search field to filter the database in the repeater:

This is part of the search/filter code (after you click on the Search button):

import wixData from 'wix-data';
import wixLocation from 'wix-location';
import {session} from 'wix-storage';


 function init() {
  ///there's some more code between these lines

async function filter() {
 $w('#textResult').text = "Searching...";
 let filterHolder = wixData.filter();
 let inSearch = $w('#inSearch').value;
 let dropMinSalePrice = $w('#dropMinSalePrice').value;
 let dropMaxSalePrice = $w('#dropMaxSalePrice').value;
 let propType = $w('#dropPropertyType').value;
 let locationSelected = $w('#dropLocation').value;
 let bedMin = $w('#dropMinBed').value;
 let bedMax = $w('#dropMaxBed').value;
 let bathMin = $w('#dropMinBath').value;
 let bathMax = $w('#dropMaxBath').value;
 let hasGarden = $w('#checkGarden').checked;
 let hasPool = $w('#checkPool').checked;
 let hasViews = $w('#checkViews').checked;
 let hasGarage = $w('#checkGarage').checked;
 let dropSaleRent = $w('#dropSaleRent').value;
 if(hasGarden) {
        filterHolder = filterHolder.eq("garden", true);
 ///there's some more code between these lines
 ///Property Type search  <<< this doesn't work (database reference field)
 if(propType && propType !== "Any") {
        filterHolder = filterHolder.eq("propertyType", propType);
///Location search  <<< this doesn't work (database reference field)
 if(locationSelected && locationSelected !== "Any") {
        filterHolder = filterHolder.eq("location", locationSelected);
 await $w('#datasetProperty').setFilter(filterHolder);

I’ve tried different methods but I can’t seem to filter the reference fields.


Is reference fields filter possible?

I sorted this search filter by replacing the value of the two dropdowns that need to filter the database from reference fields.

This is one of the dropdowns – the value now is the ‘_id’ from the referenced database collection (I could only do this because I manually inserted all the choices in the dropdown). The code above is still the same.

It’s not as dynamic as I’d like it to be but at least it’s working.

Perhaps you can use —> include for your desired filter-function…
See here…

Thanks for replying Russian-dima.
I managed to get the filter to work in way I wanted it to, but I will still see if I can make get it work with your suggestion.

I’m sure I missed something when I tried the ‘.include’ function, in the many different ways I tried it.

Which was your solution ? Working with reference-field-queries?

No, I actually used the _id of the referenced fields as the value in the dropdowns. As indicated in the screenshot above. I had manually inserted all the choices in the dropdowns, so when I used the _id as the value it worked.

I had to find a workaround solution because I had wasted a lot of time trying to find a solution, and I need to finish the website.