V10 How to count appearance of values in index

I am still struggling to convert my v4 queries to v10.

I’ve created an index that returns data sets by date:

  data: [
      date: Date("2024-04-06"),
      food: "Pizza"
      date: Date("2024-04-06"),
      food: "Burger"
      date: Date("2024-04-06"),
      food: "Burger"

I am also able to filter for distinct values.
But I can’t figure out how to combine this information to count the appearance of each “food” to get some thing like this:

[ Burger: 2, Pizza: 1 ]

Hi @panic

To get a response in v10 just like your v4 response you can project the date and food fields from the Set

Order.by_user__date_desc_food_asc(User.byId("1234")) {

assuming you have a v10 index defined like this

// FSL
collection Order {
  // other schema...

  index by_user__date_desc_food_asc {
    terms [.user]
    values [.date, .food] // same values as the v4 index

To aggregate the results, you might choose to do that client side, but if you want to do it with FQL, then you can use the fold function. Which works like Javascript reduce with the seed value.

Hi @ptpaterson

thanks for your reply.
I am able to do it locally, sure. I just wanted to do/try it in FQL for “educational” purpose.
I’ve already looked at the fold() function and other options.
But I can’t figure out the right synthax.

const arr = ['b', 'a', 'c', 'b', 'b', 'a'];

const count = arr.reduce((accumulator, value) => {
  accumulator[value] = ++accumulator[value] || 1;

  return accumulator;
}, {});

As you’ve already noted, I want to achieve something like the above with my set in fql.
But thanks for the hints. I’ll keep on trying :slight_smile:

Variables in FQL are immutable, but you can create new objects with dynamic keys and altered values using Object.assign function and the Object.fromEntries function, both of which work like the equivalent functions in JS.

let arr = ['b', 'a', 'c', 'b', 'b', 'a'];

let count = arr.fold({}, (accumulator, value) => {
  let count = (accumulator[value] ?? 0) + 1
  let override = Object.fromEntries([[value, count]])
  Object.assign(accumulator, override)



This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.