Can I add two separate field values together from same array?

Hi,

From the below results I’m trying to add the both
‘rating’ field key values, 15 + 50 together, totalling 65
‘count’ field key values, 3 + 10 together, totalling 13
I appreciate I could run separate queries but I’m trying to expand my knowledge and see how this would be possible.

This is the code I’m using to get one item to show

async function categoryRatings(){
 let stats = await wixData.query('Review-Stats').contains('category', thisCourse.title).find(); 
 if (stats) { 
 let avgRating = (Math.round(stats.rating * 10 / stats.count) / 10); 
 let percentRecommended = Math.round(stats.recommended / stats.count * 100); 
 let ratings = $w('#ratingsDisplay1');
        ratings.rating = avgRating;
        ratings.numRatings = stats.count;
        $w('#ratingsDisplay1').show(); 
        console.log('Ratings results', stats)
    } else {
        console.log('No Result')
        $w('#ratingsDisplay1').rating = null;
    }
}

Thank you :slight_smile:

Hi Stephen,

You could add those looping through the results of the query above, but WixDataAggregate with its various functions is better suited to a task like this.

wixData.aggregate("Review-Stats")
  .sum("rating")
  .sum("count")
  .run()
  .then( (results) => {
      console.log(results);
  } )

@tony-brunsman thank you, I haven’t used this part of the API before.

When trying the code, I see that there isn’t an option to filter similar to .eq or .contains to sort what fields I want to add up i.e. using something like

. eq ( “category” , “Scrambling” )

As expected, I get the total number of all of the ‘count’ and 'rating

Any ideas?

There is a filter function that can be used in aggregate queries.

@tony-brunsman , totally my bad for missing this! Have it working now.

Thanks a lot for your help.