How to query filtered dataset results?

Hey all supporters

I am trying to create a query to filtered results in the dataset.

The total count works fine in all situations and filters, but the query always returns the numbers from the entire collection.
);
#filtered_results #wixDataquery
Here is my code

wixData.query("ResearchData")
.eq("LastMesurmentCategory", "4")
.count()
.then( (num) => {
let Counter4 = num;
console.log('7-18 חודשים',Counter4);

wixData.query("ResearchData")
.eq("LastMesurmentCategory", "5")
.count()
.then( (num1) => {
let Counter5 = num1;
console.log('מעל 18 חודשים',Counter5);

$w("#ResearchData").onReady( () => {
let count = $w("#ResearchData").getTotalCount(); 
$w('#countNumberResearch').text = count.toString(); 
console.log(count);

$w("#ResearchData").onReady( () => {
let result =(Counter5 + Counter4) / count*100;
const percentage = Number.parseFloat(result).toFixed(0);
$w('#percentageText').text = percentage.toString();
console.log(percentage)
});
});
});
});
}

Hi,

An update:

The query() function does a search on your database and not dataset, as a result, it always will get you a result that include all the database’s items that match to the terms.

Because your dataset’s already filtered I suggest you to use getItems() function. (if you add more then one filter add it to the dataset’s setting).
The return will be an object that include all the dataset items that also match to the terms.
Here you can read more about getItems() function.

$w.onReady( () => {
  $w("#myDataset").onReady( () => {
    $w("#myDataset").getItems(0, "here you can write the amount of items               in your database")
      .then( (result) => {
      
    });

Moreover, instead of writing onReady() function twice, use the dataset’s onReady() function once and at the handler write the getItems() function.

*Using onReady() function more then one can get an unexpected behaviour.

Best of luck!
Sapir,

1 Like

Thanks @sapirh
And how can I query one of the fields in the dataset after I getitems?
I need just to count a specific number from one of the fields.
Thanks again,

Hi,

The resolve of the getItems() function is an object (for the explanation its name is result) and result.items is an array of objects that each one is the item from the dataset.
In order to filter this array, use JS functions such as filter() in order to get the wanted items.
Here you can read about that.

The reason I suggest you that, is because Query() function can only be activate on database, and eq() function on dataset, and also can only be used with filter() function.
As a result you will have to do filter() on your dataset twice:

  1. The general filter that you have done in the editor.
  2. In code- filter your dataset again with the filter from 1 and add eq() to filter a specific items, and then do the getItems() function.

Best,
Sapir